使用pip安装pyproject.toml依赖项时出现问题。

5

我有一个使用Poetry创建的旧项目。Poetry创建的pyproject.toml如下:

[tool.poetry]
name = "Dota2Learning"
version = "0.3.0"
description = "Statistics and Machine Learning for your Dota2 Games."
license = "MIT"
readme = "README.md"
homepage = "Coming soon..."
repository = "https://github.com/drigols/dota2learning/"
documentation = "Coming soon..."
include = ["CHANGELOG.md"]
authors = [
    "drigols <drigols.creative@gmail.com>",
]
maintainers = [
    "drigols <drigols.creative@gmail.com>",
]
keywords = [
    "dota2",
    "statistics",
    "machine Learning",
    "deep learning",
]

[tool.poetry.scripts]
dota2learning = "dota2learning.cli.main:app"

[tool.poetry.dependencies]
python = "^3.10"
requests = "^2.27.1"
typer = {extras = ["all"], version = "^0.4.1"}
install = "^1.3.5"
SQLAlchemy = "^1.4.39"
PyMySQL = "^1.0.2"
cryptography = "^37.0.4"
pydantic = "^1.9.1"
rich = "^12.5.1"
fastapi = "^0.79.0"
uvicorn = "^0.18.2"

[tool.poetry.dev-dependencies]
black = {extras = ["jupyter"], version = "^22.3.0"}
pre-commit = "^2.19.0"
flake8 = "^4.0.1"
reorder-python-imports = "^3.1.0"
pyupgrade = "^2.34.0"
coverage = "^6.4.1"

[tool.black]
line-length = 79
include = '\.pyi?$' # All Python files
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.urls]
"Bug Tracker" = "https://github.com/drigols/dota2learning/issues"

如果执行"pip install ."可以正常工作,但是现在我想采用新的方法来管理项目和依赖项,而不使用poetry。然后我手动创建了一个新的pyproject.toml文件:
[project]
name = "Dota2Learning"
version = "2.0.0"
description = "Statistics and Machine Learning for your Dota2 Games."
license = "MIT"
readme = "README.md"
homepage = ""
requires-python = ">=3.10"
repository = "https://github.com/drigols/dota2learning/"
documentation = ""
include = ["CHANGELOG.md"]
authors = [
    "drigols <drigols.creative@gmail.com>",
]
maintainers = [
    "drigols <drigols.creative@gmail.com>",
]
keywords = [
    "dota2",
    "statistics",
    "machine Learning",
    "deep learning",
]

dependencies = [
    "requests>=2.27.1",
    "typer>=0.4.1",
    "SQLAlchemy>=1.4.39",
    "PyMySQL>=1.0.2",
    "cryptography>=37.0.4",
    "pydantic>=1.9.1",
    "rich>=12.5.1",
    "fastapi>=0.79.0",
    "uvicorn>=0.18.2",
]

[project.optional-dependencies]
# Dev dependencies.
dev = [
    "black>=22.3.0",
    "pre-commit>=2.19.0",
    "flake8>=4.0.1",
    "reorder-python-imports>=3.1.0",
    "pyupgrade>=2.34.0",
]
# Testing dependencies.
test = [
    "coverage>=6.4.1",
]
# Docs dependencies.
doc = []

[project.scripts]
dota2learning = "dota2learning.cli.main:app"

[tool.black]
line-length = 79
include = '\.pyi?$' # All Python files
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''

问题现在是pip命令"pip install ."不起作用:
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [89 lines of output]
      configuration error: `project.license` must be valid exactly by one definition (2 matches found):
      
          - keys:
              'file': {type: string}
            required: ['file']
          - keys:
              'text': {type: string}
            required: ['text']
      
      DESCRIPTION:
          `Project license <https://www.python.org/dev/peps/pep-0621/#license>`_.
      
      GIVEN VALUE:
          "MIT"
      
      OFFENDING RULE: 'oneOf'
      
      DEFINITION:
          {
              "oneOf": [
                  {
                      "properties": {
                          "file": {
                              "type": "string",
                              "$$description": [
                                  "Relative path to the file (UTF-8) which contains the license for the",
                                  "project."
                              ]
                          }
                      },
                      "required": [
                          "file"
                      ]
                  },
                  {
                      "properties": {
                          "text": {
                              "type": "string",
                              "$$description": [
                                  "The license of the project whose meaning is that of the",
                                  "`License field from the core metadata",
                                  "<https://packaging.python.org/specifications/core-metadata/#license>`_."
                              ]
                          }
                      },
                      "required": [
                          "text"
                      ]
                  }
              ]
          }
      Traceback (most recent call last):
        File "/home/drigols/Workspace/dota2learning/environment/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 351, in <module>
          main()
        File "/home/drigols/Workspace/dota2learning/environment/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 333, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/home/drigols/Workspace/dota2learning/environment/lib/python3.10/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 338, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 320, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 484, in run_setup
          super(_BuildMetaLegacyBackend,
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 335, in run_setup
          exec(code, locals())
        File "<string>", line 1, in <module>
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/__init__.py", line 87, in setup
          return distutils.core.setup(**attrs)
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 159, in setup
          dist.parse_config_files()
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/dist.py", line 867, in parse_config_files
          pyprojecttoml.apply_configuration(self, filename, ignore_option_errors)
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/config/pyprojecttoml.py", line 62, in apply_configuration
          config = read_configuration(filepath, True, ignore_option_errors, dist)
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/config/pyprojecttoml.py", line 126, in read_configuration
          validate(subset, filepath)
        File "/tmp/pip-build-env-up7_m__d/overlay/lib/python3.10/site-packages/setuptools/config/pyprojecttoml.py", line 51, in validate
          raise ValueError(f"{error}\n{summary}") from None
      ValueError: invalid pyproject.toml config: `project.license`.
      configuration error: `project.license` must be valid exactly by one definition (2 matches found):
      
          - keys:
              'file': {type: string}
            required: ['file']
          - keys:
              'text': {type: string}
            required: ['text']
      
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

这就像pip不知道如何从pyproject.toml安装依赖项,而不像poetry的方法,我不明白为什么。

问题已解决! 我编辑了pyproject.toml文件:

[build-system]
# Minimum requirements for the build system to execute.
requires = ["setuptools", "wheel"]  # PEP 508 specifications.
build-backend = "setuptools.build_meta"

# Ignore flat-layout.
[tool.setuptools]
py-modules = []

[project]
name = "Dota2Learning"
version = "2.0.0"
description = "Statistics and Machine Learning for your Dota2 Games."
license = {file = "LICENSE.md"}
readme = "README.md"
requires-python = ">=3.10.0"
authors = [
    { name = "Rodrigo Leite", email = "drigols.creative@gmail.com" },
]
maintainers = [
    { name = "Rodrigo Leite", email = "drigols.creative@gmail.com" },
]
keywords = [
    "dota2",
    "statistics",
    "machine Learning",
    "deep learning",
]

dependencies = [
    "requests>=2.27.1",
    "typer>=0.4.1",
    "SQLAlchemy>=1.4.39",
    "PyMySQL>=1.0.2",
    "cryptography>=37.0.4",
    "pydantic>=1.9.1",
    "rich>=12.5.1",
    "fastapi>=0.79.0",
    "uvicorn>=0.18.2",
]

[project.optional-dependencies]
# Dev dependencies.
dev = [
    "black>=22.3.0",
    "pre-commit>=2.19.0",
    "flake8>=4.0.1",
    "reorder-python-imports>=3.1.0",
    "pyupgrade>=2.34.0",
]
# Test dependencies.
test = [
    "coverage>=6.4.1",
]
# Doc dependencies.
doc = []

[project.scripts]
# dota2learning = "dota2learning.cli.main:app"

[tool.black]
line-length = 79
include = '\.pyi?$' # All Python files
exclude = '''
/(
    \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''

你想放弃使用Poetry作为项目的构建后端并切换到另一个构建后端吗?如果是,那么你想选择哪一个? - sinoroc
1个回答

3

多个问题:

1. 正如错误消息清楚地说明的那样,在[project]部分下,license密钥存在问题。它的值应该是一个表格。请参见规范

2. 您展示给我们的新pyproject.toml文件缺少[build-system]部分。如果没有这个部分,那么构建前端(如pip)将假定此项目的构建后端是setuptools(参见PEP 517PEP 518),而不是您想要的poetry/poetry-core

3. 截至今天,Poetry与pyproject.toml文件中的这个新的[project]部分不兼容。Poetry尚未实现PEP 621更改,因此无论如何都不起作用,除非项目从Poetry更改其构建后端到另一个构建后端。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接