我正在使用setuptools
开发一个Python包。我使用 src 布局使用 setup.cfg
文件设置配置。在 Travis-CI 上的 Python 3.6 上,我得到了一个ModuleNotFoundError
。
src 布局提供以下结构:
my-package
├── setup.cfg
├── setup.py
└── src
└── my_package
├── my_file.py
└── version.py
如文档中所述,我使用 attr:
说明符获取版本。我还使用 extras_require
键来获取一些额外的依赖项(特别是对于拥有测试工具可执行文件(。
[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:
说明符。此功能可以在 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
文件中的先决条件。
我在使用 setuptools 46.x 时遇到了同样的错误,但我的设置.cfg略有不同。这部分很重要:
[options]
package_dir =
=src
packages = find:
[options.packages.find]
where = src