简体   繁体   English

requirements.txt中的安装时依赖项

[英]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. 我正在使用tox来准备venv并运行单元测试,我的应用程序需要openopt库,而后者又在其setup.py中导入numpy.distutils.core

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 无论我如何在我的requirements.txt中命令numpy和openopt我都无法确保在执行openopt的setup.py之前安装numpy并使用ImportError: No module named numpy.distutils.core退出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. 对于开发,我可以将numpy添加到requirements.txt,运行tox,添加openopt并再次运行tox,但它不是生产就绪的设置。

UPDATE There is an issue in the tox project that might be implemented that would add functionality to deal with these kinds of problems in a more "official" way. 更新 tox项目中存在一个问题,可能会以更“官方”的方式添加处理这类问题的功能。 Discussion is here: Add an option to run commands after virtualenv creation but before other steps 讨论在这里: 添加一个选项,以便在virtualenv创建之后但在其他步骤之前运行命令

UPDATE (a bit more background): The main problem is that it is a BadThing(TM) to assume that some other package is installed already in setup.py . 更新(更多背景):主要问题是假设在setup.py已经安装了一些其他软件包,这是BadThing(TM)。 these kinds of problems fall into the area of bootstrapping and they can be hellish to handle properly, but usually this is possible with some extra effort. 这些问题属于自举的范围 ,它们可能会很难处理,但通常可以通过一些额外的努力来实现。 If you really need a different package at setup time, you can look into setup_requires and some additional magic (have a look eg at setuptools_scm for inspiration). 如果你在设置时真的需要一个不同的包,你可以查看setup_requires和一些额外的魔法(看看例如在setuptools_scm的灵感)。 In the worst case and if the package is not to complicated, you can make it part of your package (which comes with its own problems though, like keeping it up to date and possible licensing conflicts). 在最糟糕的情况下,如果软件包不复杂,你可以将它作为软件包的一部分(虽然它有自己的问题,比如保持最新和可能的许可冲突)。

Original answer : 原始答案

If you use requirements.txt already, an easy (but admittedly ugly) solution would be: 如果你已经使用了requirements.txt ,那么一个简单(但不可否认的丑陋)的解决方案是:

  1. create two (or more) requirements files (eg requirements-0.txt and requirements-1.txt (hopefully with better names)). 创建两个(或更多)需求文件(例如, requirements-0.txtrequirements-1.txt (希望有更好的名称))。
  2. sort the packages by dependency into those files 按顺序将包排序到这些文件中
  3. use commands instead of deps to install them in the right order 使用命令而不是deps以正确的顺序安装它们

.eg 。例如

[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 ...
    pytest tests/unit

... or if you want to keep it even simpler, just stick the package that is being imported in setup.py of another package right in front of your single requirements.txt ...或者如果你想让它更简单,只需将正在导入的软件包放在另一个软件包的setup.py ,就在你的单个requirements.txt前面

[...]
commands =
    pip install <package that needs to be installed first (e.g. numpy)>
    pip install -r {toxinidir}/requirements.txt        
    pytest tests/unit

It's documented in https://testrun.org/tox/latest/example/basic.html#depending-on-requirements-txt 它记录在https://testrun.org/tox/latest/example/basic.html#depending-on-requirements-txt中

deps = -rrequirements.txt

According to common practice on github, common trick is: 根据github上的常规做法,常见的诀窍是:

deps =
    setuptools
    -r{toxinidir}/requirements.txt

I have a generic way to bootstrap build-time dependencies in setup.py . 我有一个通用的方法来引导setup.py构建时依赖项。 You can use this even if you are not using tox . 即使您没有使用tox也可以使用它。 For this case, add the following snippet to the top of the setup.py script. 对于这种情况,请将以下代码段添加到setup.py脚本的顶部。

from setuptools.dist import Distribution

# Bootstrapping dependencies required for the setup
Distribution(dict(setup_requires=['numpy']))

Warning : This will install numpy using easy_install . 警告 :这将使用easy_install安装numpy Installing numpy with this method is somewhat tricky. 使用这种方法安装numpy有点棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM