使用Poetry导出requirements.txt时如何省略依赖

Rul*_*lle 7 python pip requirements.txt python-poetry

我有一个 Python3 Poetry 项目,其中有一个pyproject.toml指定依赖项的文件:

[tool.poetry.dependencies]
python = "^3.10"
nltk = "^3.7"
numpy = "^1.23.4"
scipy = "^1.9.3"
scikit-learn = "^1.1.3"
joblib = "^1.2.0"

[tool.poetry.dev-dependencies]
pytest = "^5.2"
Run Code Online (Sandbox Code Playgroud)

requirements.txt使用命令将这些依赖项导出到文件中poetry export --without-hashes -f requirements.txt --output requirements.txt,生成以下文件requirements.txt

click==8.1.3 ; python_version >= "3.10" and python_version < "4.0"
colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows"
joblib==1.2.0 ; python_version >= "3.10" and python_version < "4.0"
nltk==3.8.1 ; python_version >= "3.10" and python_version < "4.0"
numpy==1.24.1 ; python_version >= "3.10" and python_version < "4.0"
regex==2022.10.31 ; python_version >= "3.10" and python_version < "4.0"
scikit-learn==1.2.1 ; python_version >= "3.10" and python_version < "4.0"
scipy==1.9.3 ; python_version >= "3.10" and python_version < "4.0"
threadpoolctl==3.1.0 ; python_version >= "3.10" and python_version < "4.0"
tqdm==4.64.1 ; python_version >= "3.10" and python_version < "4.0"
Run Code Online (Sandbox Code Playgroud)

我在构建 Docker 镜像时用它来安装依赖项。

我的问题:调用时如何省略colorama上述要求列表中的依赖项poetry export --without-hashes -f requirements.txt --output requirements.txt

可能的解决方案:colorama我可以通过使用 生成requirements.txt文件来过滤掉该行poetry export --without-hashes -f requirements.txt | grep -v colorama > requirements.txt。但这似乎很棘手,并且如果 Colorama 要求在该文件中的多行中表达,则可能会破坏事情。有没有更好、更少黑客的方法?

背景:在使用构建 Docker 映像的同时安装此要求列表时,pip install -r requirements.txt我收到消息

Ignoring colorama: markers 'python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows"' don't match your environment
Run Code Online (Sandbox Code Playgroud)

一位同事认为该消息看起来很难看,并且希望它不可见(但我个人不在乎)。调用poetry show --tree表明 Colorama 依赖项是 Windows 所必需的,pytest并且用于使终端颜色在 Windows 上工作。在 Linux 上安装时省略库作为要求在这种情况下不太可能出现问题。

小智 5

依赖项colorama是 所必需的pytest

我想您的 Docker 映像是用于生产用途的,因此它不必包含pytest明显的开发依赖项。

您可以使用poetry export --without-hashes --without dev -f requirements.txt -o requirements.txt来阻止您的dev包导出到该requirements.txt文件,这样它们就不会被pip install.

您可以在这里找到一些poetry export选项。