从文件加载的版本不符合 PEP 440

Version loaded from file does not comply with PEP 440

如标题所述,当我 运行 python -m build 在本地使用版本文件时出现此错误。我已经阅读了 PEP 440 and to me, it appears compliant. I cannot see from the documentation on setuptools 为什么我会 运行 关注这个问题

我真的很感激在这方面的一些帮助,因为我不知道我如何能够 运行 在 python -m build 上进行调试,或者更深入地了解为什么 _parse_version setuptools/config.py 的 ] 对我的版本文件不满意。

更多详情:

我的项目结构如下:

src/
   mypackage/
      __init__.py
      VERSION
pyproject.toml
setup.cfg
setup.py

版本

0.0.1

setup.cfg

name = mypackage
version = file: src/mypackage/VERSION
# etc. etc. etc.

setup.py

#!/usr/bin/env python
# Used to create editable installs
import setuptools

setuptools.setup()

pyproject.toml

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

有关错误的更多详细信息:

  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/build_meta.py", line 154, in get_requires_for_build_wheel
    return self._get_build_requires(
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/build_meta.py", line 135, in _get_build_requires
    self.run_setup()
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/build_meta.py", line 150, in run_setup
    exec(compile(code, __file__, 'exec'), locals())
  File "setup.py", line 5, in <module>
    setuptools.setup()
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/__init__.py", line 153, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.8/distutils/core.py", line 121, in setup
    dist.parse_config_files()
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/_virtualenv.py", line 21, in parse_config_files
    result = old_parse_config_files(self, *args, **kwargs)
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/dist.py", line 778, in parse_config_files
    parse_configuration(self, self.command_options,
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/config.py", line 157, in parse_configuration
    meta.parse()
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/config.py", line 463, in parse
    section_parser_method(section_options)
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/config.py", line 436, in parse_section
    self[name] = value
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/config.py", line 220, in __setitem__
    value = parser(value)
  File "/tmp/build-env-2kboopyp/lib/python3.8/site-packages/setuptools/config.py", line 553, in _parse_version
    raise DistutilsOptionError(tmpl.format(**locals()))
distutils.errors.DistutilsOptionError: Version loaded from file: src/mypackage/VERSION does not comply with PEP 440: 

ERROR Backend subproccess exited when trying to invoke get_requires_for_build_wheel

虽然我对当前的文档或错误消息不满意,但我已经解决了这个问题,就目前而言,我觉得很容易落入这个陷阱。

所以解决方案是创建一个 MANIFEST.in 文件,其中包含以下行: include src/mypackage/VERSION 在我的回购协议的顶层:

src/
   mypackage/
      __init__.py
      VERSION
pyproject.toml
setup.cfg
setup.py
MANIFEST.in

此外,必须将以下内容添加到 setup.cfg

[options]
include_package_data = True

来自docs

include_package_data If set to True, this tells setuptools to automatically include any data files it finds inside your package directories that are specified by your MANIFEST.in file. For more information, see the section on Including Data Files.

setuptools documentation V57.1.0 让我相信它会包含在内:

Automatically include all relevant files in your source distributions, without needing to create a MANIFEST.in file, and without having to force regeneration of the MANIFEST file when your source tree changes.

直觉上我会认为带有路径的 file: 关键字就足够了,或者结果是因为没有这样的文件而产生的错误消息可能让我明白了多一点。这在 packaging-projects

中也不明显

但我想可以从MANIFEST.in documentation推断出来。

我说这一切是因为万一有人有任何有用的东西可以补充或反驳我目前的感觉:当前指南中可能有关于使用版本文件的遗漏。