在 pyproject.toml 中指定测试依赖项,并使用 pip install -e 安装它们。

huangapple go评论66阅读模式
英文:

Specifying test dependencies in pyproject.toml and getting them installed with pip install -e

问题

我有一个Python项目,其中有一个类似于下面的pyproject.toml文件。我使用python build来生成一个包,我在生产环境中安装使用。对于开发环境,我使用pip install -e .

我正在尝试弄清楚如何确保在开发环境中安装测试依赖项,但不包括在生产环境中。

[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
package-dir = {"" = "src"}
packages = [
    "package-a",
    "package-b",
]

[tool.setuptools.package-data]
"*" = [
    "path/to/*.txt",
]

[project]
name = "my-project"
version = "0.0.1"

authors = [
  { name="devnectar", email="a@b.com" },
]

description = "description goes here"
readme = "README.md"
requires-python = ">=3.10"

classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

dependencies = [
    "dep",
    "another-dep",
    "yet-another-dep",
]

[project.optional-dependencies]
dev = [
    "tox",
]

[project.urls]
"Homepage" = "https://some_url.com"
"Bug Tracker" = "https://some_url.com"

[project.scripts]
nectarserver = "entry-point-script"

[tool.tox]
legacy_tox_ini = """
    [tox]
    min_version = 4.0
    env_list = test_env

    [testenv]
    deps = pytest
    commands = pytest -s
"""

我还尝试过在尝试使其工作时使用testtests代替dev

我遇到了两个问题:

  • 当我运行pip install -e .时,tox未安装。
  • install命令生成的requires.txt文件最终会有一个[dev]部分,这会导致pip install -r mypackage/mypackage.egg-info/requires.txt出错。这在我的构建链中的后续步骤中引发了其他问题,因为它依赖于在构建期间生成的requirements.txt文件,以生成包含项目依赖项的Docker层。

我应该如何在我的pyproject.toml中捕获仅用于开发的依赖项tox

英文:

I have a Python project with a pyproject.toml similar to the one below. I use python build to generate an package I use to install in production environments. For development environments, I use pip install -e .

I'm trying to figure out how to make sure test dependencies are installed for development environments, but not as part of a production environment.

[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
package-dir = {"" = "src"}
packages = [
    "package-a",
    "package-b",
]

[tool.setuptools.package-data]
"*" = [
    "path/to/*.txt"
]

[project]
name = "my-project"
version = "0.0.1"

authors = [
  { name="devnectar", email="a@b.com" },
]

description = "description goes here"
readme = "README.md"
requires-python = ">=3.10"

classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

dependencies = [
    "dep",
    "another-dep",
    "yet-another-dep"
]

[project.optional-dependencies]
dev = [
    "tox"
]

[project.urls]
"Homepage" = "https://some_url.com"
"Bug Tracker" = "https://some_url.com"

[project.scripts]
nectarserver = "entry-point-script"

[tool.tox]
legacy_tox_ini = """
    [tox]
    min_version = 4.0
    env_list = test_env

    [testenv]
    deps = pytest
    commands = pytest -s
"""

I also tried test and tests instead of dev when trying to get this to work.

I'm running into two issues:

  • When I run pip install -e ., tox is not installed
  • The requires.txt generated by the install command ends up having a [dev] section in it, which causes pip install -r mypackage/mypackage.egg-info/requires.txt to error out. This causes other issues down the road in my build chain, which relies on requirements.txt files generated during the build to generate a Docker layer with project dependencies in it.

How should I capture the dev only dependency on tox in my pyproject.toml?

答案1

得分: 10

额外依赖 对于本地包与 PyPI 上的包的工作方式相同;请尝试

pip install -e .[dev]
英文:

Extra dependencies work the same for local packages as they do for packages on PyPI; try

pip install -e .[dev]

huangapple
  • 本文由 发表于 2023年1月9日 00:56:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定