gpt4 book ai didi

python - requirements.txt 中的安装时依赖项

转载 作者:行者123 更新时间:2023-11-28 16:26:01 26 4
gpt4 key购买 nike

我正在使用 tox 准备 venv 并运行单元测试,我的应用程序需要 openopt 库,该库又导入 numpy.distutils.core在它的 setup.py 中。

无论我如何在我的 requirements.txt 中订购 numpy 和 openopt,我都无法确保在执行来自 openopt 的 setup.py 并以 ImportError: No module named numpy.distutils.core 退出之前安装 numpy。

我该如何解决?对于开发,我可以将 numpy 添加到 requirements.txt,运行 tox,添加 openopt 并再次运行 tox,但这不是生产就绪的设置。

最佳答案

更新 可能实现的 tox 项目中存在一个问题,该问题将添加功能以更“正式”的方式处理这些类型的问题。讨论在这里:Add an option to run commands after virtualenv creation but before other steps

更新(更多背景信息): 主要问题是假设已经在 setup.py 中安装了一些其他包是一个 BadThing(TM) .这些类型的问题属于 bootstrapping 领域正确处理它们可能会很糟糕,但通常需要额外的努力才能做到这一 pip 。如果您真的在设置时需要不同的包,您可以查看setup_requires 和一些额外的魔法(查看例如 setuptools_scm 以获得灵感)。在最坏的情况下,如果包不是太复杂,您可以将它作为包的一部分(尽管它有自己的问题,比如保持最新和可能的许可冲突)。

原始答案:

如果您已经使用了 requirements.txt,一个简单(但公认的丑陋)的解决方案是:

  1. 创建两个(或更多)需求文件(例如 requirements-0.txtrequirements-1.txt(希望名称更好)。
  2. 根据依赖性将包排序到这些文件中
  3. 使用commands而不是 deps以正确的顺序安装它们

.例如

[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

...或者如果你想让它更简单,只需将正在导入的包粘贴到另一个包的 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

关于python - requirements.txt 中的安装时依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36574463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com