gpt4 book ai didi

python-3.x - 在 src 布局中使用 attr 时出现 ModuleNotFoundError

转载 作者:行者123 更新时间:2023-12-02 19:56:00 25 4
gpt4 key购买 nike

我正在使用setuptools开发一个Python包。我使用 setup.cfg file 设置配置,使用 src 布局。在 Travis-CI 上的 Python 3.6 上,我收到 ModuleNotFoundError

src 布局给出以下结构:

my-package
├── setup.cfg
├── setup.py
└── src
└── my_package
├── my_file.py
└── version.py

正如文档中所解释的,我使用 attr: 说明符获取版本。我还使用 extras_require 键来获取一些额外的依赖项 ( especially for having test tool executables )。

[metadata]
name = mypackage
version = attr: my_package.version.__version__

[options]
package_dir =
=src
packages = find:

[options.packages.find]
where = src

[options.extras_require]
test =
dummy

然后,我使用 pip 安装依赖项:

pip install -e ".[test]"

这可以在使用 Python 3.7 的本地环境和使用 Python 3.5 的 Travis-CI 环境中无缝运行。但在使用 Python 3.6 的 Travis-CI 环境中,我收到一个 ModuleNotFoundError:

Obtaining file:///home/travis/build/me/my-package
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/travis/build/me/my-package/setup.py", line 4, in <module>
setup()
File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
return distutils.core.setup(**attrs)
File "/opt/python/3.6.3/lib/python3.6/distutils/core.py", line 121, in setup
dist.parse_config_files()
File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/setuptools/dist.py", line 436, in parse_config_files
ignore_option_errors=ignore_option_errors)
File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/setuptools/config.py", line 106, in parse_configuration
meta.parse()
File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/setuptools/config.py", line 382, in parse
section_parser_method(section_options)
File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/setuptools/config.py", line 355, in parse_section
self[name] = value
File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/setuptools/config.py", line 173, in __setitem__
value = parser(value)
File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/setuptools/config.py", line 428, in _parse_version
version = self._parse_attr(value)
File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/setuptools/config.py", line 305, in _parse_attr
module = import_module(module_name)
File "/home/travis/virtualenv/python3.6.3/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'my_package'

出了什么问题?

最佳答案

Travis-CI 为 Python 3.6 提供的 setuptools 包似乎太旧,无法正确使用 attr: 说明符。这个功能可以用in v39.2.0 ,而 Travis-CI Python 3.6 提供 v38.2.4(因此出现错误),而 Travis-CI Python 3.5 提供 40.0.0(因此成功)。

所以,我只是输入:

pip install --upgrade "setuptools>=40.0"

作为我的 .travis-ci.yaml 文件中的先决条件。

关于python-3.x - 在 src 布局中使用 attr 时出现 ModuleNotFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57033921/

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