Chulup Chulup - 3 months ago 28
Python Question

Install-time dependencies in requirements.txt

I'm using tox to prepare venv and run unit tests and my application needs openopt library which in turn imports

numpy.distutils.core
in its setup.py.

No matter how I order numpy and openopt in my requirements.txt I can't ensure numpy is installed before setup.py from openopt is executed and exit with
ImportError: No module named numpy.distutils.core


How can I fix that? For development I can add numpy to requirements.txt, run tox, add openopt and run tox again but it's not production-ready setup.

Answer

If you use requirements.txt already, an easy (but admittedly ugly) solution would be:

  1. create two (or more) requirements files (e.g. requirements-0.txt and requirements-1.txt (hopefully with better names)).
  2. sort the packages by dependency into those files
  3. use commands instead of deps to install them in the right order

.e.g.

[testenv]
deps = 
    pytest
    # whatever else where order does not matter

commands =
    pip install -r {toxinidir}/requirements-0.txt
    pip install -r {toxinidir}/requirements-1.txt
    # ... and more if needed

    # now do your actual testing ...
    py.test tests/unit

... or if you want to keep it even simpler, just stick the offending package right in front of your single requirements.txt

[...]
commands =
    pip install <nasty dependency>
    pip install -r {toxinidir}/requirements.txt        
    py.test tests/unit