编辑:有关我的错误解释和解决方案,请参阅下面的答案
我试图按照@dnozay关于如何从文件中读取setup.py中的__version__
的回答中的说明进行操作。
我的(简化的)项目文件树是:
setup.py
my_module
├── __init.py__
├── _version.py
└── myclasses.py
按照答案指示,我在_version.py
:中公开__version__
# _version.py
_version = {
'major': 1,
'minor': 0,
'revis': 0,
}
__version__ = '.'.join([str(a) for a in _version.values()])
而我的setup.py
包含:
from setuptools import setup, find_packages
from distutils.util import convert_path
main_ns = {}
ver_path = convert_path("my_module/_version.py")
with open(ver_path) as ver_file:
exec(ver_file.read(), main_ns)
setup(...
version=main_ns['__version__'],
package_dir={"": "my_module"},
packages=find_packages(where="my_module"),
...)
然而,当我尝试用python3 -m build
构建包时,我得到了FileNotFoundError
* Building wheel from sdist
* Creating virtualenv isolated environment...
* Installing packages in isolated environment... (setuptools>=45)
* Getting dependencies for wheel...
Traceback (most recent call last):
File "/home/username/.local/lib/python3.8/site-packages/pep517/in_process/_in_process.py", line 363, in <module>
main()
File "/home/username/.local/lib/python3.8/site-packages/pep517/in_process/_in_process.py", line 345, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/home/username/.local/lib/python3.8/site-packages/pep517/in_process/_in_process.py", line 130, in get_requires_for_build_wheel
return hook(config_settings)
File "/tmp/build-env-xlvmof6k/lib/python3.8/site-packages/setuptools/build_meta.py", line 177, in get_requires_for_build_wheel
return self._get_build_requires(
File "/tmp/build-env-xlvmof6k/lib/python3.8/site-packages/setuptools/build_meta.py", line 159, in _get_build_requires
self.run_setup()
File "/tmp/build-env-xlvmof6k/lib/python3.8/site-packages/setuptools/build_meta.py", line 174, in run_setup
exec(compile(code, __file__, 'exec'), locals())
File "setup.py", line 10, in <module>
with open(ver_path) as ver_file:
FileNotFoundError: [Errno 2] No such file or directory: 'my_module/_version.py'
好吧,我明白我的错误了。find_packages()
函数试图在my_package/my_package/
中查找模块文件,这就是构建失败的原因,my_package/my_package/_version.py
不存在。
为了修复它,我将setup()
更改为:
setup=(...
version=main_ns['__version__'],
packages=find_packages(where='.')
...)