TL;DR
我正在尝试为一个使用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'.下面是目录树的简单视图(不展开.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。回购是干净的和标记的。使用setuptools 49.1.0和setuptools_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.2pyproject.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时更新root和relative_to参数,并尝试更新use_scm_version以使用配置字典,但都无济于事。我在github中调查了官方问题,但我无法解决这个问题。在这一点上,我确信我误解了setuptools_scm,并且可能对它进行了错误的配置。
发布于 2020-07-07 07:18:52
您不应该在实际代码中使用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'https://stackoverflow.com/questions/62730254
复制相似问题