gpt4 book ai didi

python-3.x - 运行使用 setuptools_scm 从 bdist_wheel 包安装的应用程序失败并出现版本查找错误

转载 作者:行者123 更新时间:2023-12-04 15:23:47 25 4
gpt4 key购买 nike

长话短说

我正在尝试为使用 setuptools-scm 的项目创建内置分发包。我生成了运行 python setup.py sdist bdist_wheel 的 wheel 包。我可以使用 pip install ... 安装 .whl 包。但是当我尝试运行该程序时,它会抛出以下错误:

Traceback (most recent call last):
File "/home/zobayer/Projects/opensource/staging/myproject/venv/bin/mymodule", line 5, in <module>
from mymodule.cli import main
File "/home/zobayer/Projects/opensource/staging/myproject/venv/lib64/python3.8/site-packages/mymodule/cli.py", line 4, in <module>
from mymodule.contexts import ContextConfig
File "/home/zobayer/Projects/opensource/staging/myproject/venv/lib64/python3.8/site-packages/mymodule/contexts/__init__.py", line 2, in <module>
from mymodule.contexts.context_config import ContextConfig
File "/home/zobayer/Projects/opensource/staging/myproject/venv/lib64/python3.8/site-packages/mymodule/contexts/context_config.py", line 8, in <module>
from mymodule.commons import local_config_file_path, user_config_file_path
File "/home/zobayer/Projects/opensource/staging/myproject/venv/lib64/python3.8/site-packages/mymodule/commons/__init__.py", line 19, in <module>
version = get_version(root="../..", relative_to=__file__)
File "/home/zobayer/Projects/opensource/staging/myproject/venv/lib64/python3.8/site-packages/setuptools_scm/__init__.py", line 144, in get_version
return _get_version(config)
File "/home/zobayer/Projects/opensource/staging/myproject/venv/lib64/python3.8/site-packages/setuptools_scm/__init__.py", line 148, in _get_version
parsed_version = _do_parse(config)
File "/home/zobayer/Projects/opensource/staging/myproject/venv/lib64/python3.8/site-packages/setuptools_scm/__init__.py", line 110, in _do_parse
raise LookupError(
LookupError: setuptools-scm was unable to detect version for '/home/zobayer/Projects/opensource/staging/myproject/venv/lib64/python3.8/site-packages'.

这是目录树的简单 View (没有展开 .git\)

myproject
├── .git
├── mymodule
│   ├── cli.py
│   ├── commons
│   │   ├── command.py
│   │   ├── helpers.py
│   │   └── __init__.py
│   ├── contexts
│   │   ├── context_config.py
│   │   └── __init__.py
│   ├── __init__.py
│   └── version.py
├── LICENSE
├── MANIFEST.in
├── pyproject.toml
├── README.md
├── requirements.txt
├── setup.cfg
├── setup.py
└── tox.ini

这里 mymodule/version.py 没有被跟踪。 repo 协议(protocol)是干净的和标记的。使用 setuptools 49.1.0setuptools_scm 4.1.2,我配置了以下内容:

setup.py(省略元数据字段)

setup(
name="myproject",
use_scm_version=True,
setup_requires=["wheel", "setuptools-scm"],
packages=find_packages(exclude=["docs*", "tests*"]),
include_package_data=True,
install_requires=install_dependencies,
)

setup.cfg

[metadata]
license_files = LICENSE

[options]
setup_requires =
wheel==0.34.2
setuptools_scm==4.1.2

pyproject.toml

[build-system]
requires = ["setuptools>=42", "wheel", "setuptools_scm[toml]>=3.4"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
write_to = "mymodule/version.py"
write_to_template = "# -*- coding: utf-8 -*-\n\n__version__ = '{version}'\n"
version_scheme = "release-branch-semver"

我想在这里指出,安装与 pip install -e 一起工作。 并且应用程序按预期运行。

我尝试在调用 get_version 时更新 rootrelative_to 参数,并更新 use_scm_version 以使用配置词典无用。我查看了github中的官方问题,但我无法解决这个问题。在这一点上,我确信我误解了 setuptools_scm 并且可能配置错误。

最佳答案

您不应该在实际代码中使用 setuptools-scm,它仅用于打包。它寻找 VCS 存储库以从其元数据(git 标签等)中读取版本,因此很明显它会在安装包时失败。如果您尝试获取已安装软件包的版本,请使用 importlib.metadata (自 3.8 起成为标准库的一部分,对于旧版本,有一个向后移植:importlib-metadata)。示例:

>>> from importlib.metadata import version
>>> version('myproject')
'0.0.1'

您还可以使用 pkg_resources(setuptools 的一部分)来查询版本元数据,但请注意它已被 importlib.metadata 取代现在:

>>> import pkg_resources
>>> pkg_resources.get_distribution('myproject').version
'0.0.1'

关于python-3.x - 运行使用 setuptools_scm 从 bdist_wheel 包安装的应用程序失败并出现版本查找错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62730254/

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