Can I define additional packages in setuptools?

Currently, one of my packages requires a JSON analyzer / encoder, and it is intended to be used simplejson, if possible, if necessary, it will be returned back to the module json(in the standard library) (as tests show simplejsonfaster).

However, recently it has been amazed and it is not indicated whether it will simplejsoninstall when using zc.buildout- something with the transition to github, I suppose. What made me think; Is it possible to identify optional packages in my file setup.py, which, if it is not available, will not stop the installation of my package?

+5
source share
2 answers

.

, setup.py script. , :

# mypackage/setup.py

extras = {
   'with_simplejson': ['simplejson>=3.5.3']
}

setup(
    ...
    extras_require=extras,
    ...)

:

  • pip install mypackage,
  • pip install mypackage[with_simplejson]

simplejson>=3.5.3.

, , , , , .

.

, , , . . json:

try:
    # helpful comment saying this should be faster.
    import simplejson as json
except ImportError:
    import json

:

try:
    # xml is dangerous
    from defusedxml.cElementTree import parse
except ImportError:
    try:
        # cElementTree is not available in older python
        from xml.cElementTree import parse
    except ImportError:
        from xml.ElementTree import parse

:

try:
    optional_package = None
    import optional.package as optional_package
except ImportError:
    pass

...

if optional_package:
    # do addtional behavior
+6

AFAIK , . , ? , ? ( - )

, - , . :

try:
    from somespecialpackage import someapi as myapi
except ImportError:
    from basepackage import theapi as myapi

, , , API , simplejson json.

0

All Articles