Tutorial :setuptools “at least one of these” dependency specification



Question:

In some cases, there are various modules which each implement a common API (in my case, the old pure-python elementtree, cElementTree, lxml.etree, and the built-in xml.etree). I can write the module using ElementTree to try each of these options, and take the first one that exists according to my own preference order -- but I can't find any information on specifying that only one of these must be installed in setup.py. I want to write something that looks like this:

setup(      ...,      install_requires="""          elementtree | cElementTree | lxml      """,      ...  )  

Is this, or something like it, possible?


Solution:1

I don't think so, but, if you're using a reasonably recent Python, elementtree being part of the standard Python libraries, why do you worry it might be absent? (I do understand this would be a problem for other cases of several possible implementations of an API, I just wonder if you really need it for your specific use case).


Solution:2

You might want to try doing something like this in setup.py:

install_requires = ["elementree"]    try:      import cElementTree      install_requires = []  except ImportError:      try:          import lxml          install_requires = []      except ImportError:          # etc for remaining equivalent modules    setup(      install_requires = install_requires,      # rest of setup parameters  )  

This will basically install elementree package as dependency if none of the equivalent are installed.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »