python-3.x - 'extras_require' 必须是一个字典,其值是包含有效项目/版本要求说明符的字符串或字符串列表
问题描述
我有一个setup.py
包含以下内容:
from pip._internal.req import parse_requirements
def load_requirements(fname):
"""Turn requirements.txt into a list"""
reqs = parse_requirements(fname, session="test")
return [str(ir.requirement) for ir in reqs]
setup(
name="Projectname",
[...]
python_requires='>=3.6',
extras_require={
'dev': load_requirements('./requirements/dev.txt')
},
install_requires=load_requirements('./requirements/prod.txt')
)
我的./requirements/prod.txt
样子是这样的:
-r common.txt
和我./requirements/dev.txt
的类似,但有一些特定于开发的包。My./requirements/common.txt
包含从 github 链接 pip-install 软件包的行,例如:
-e git://github.com/BioGeek/tta_wrapper.git@master#egg=tta_wrapper
但是,由于我添加了该行,因此命令python setup.py build
失败并显示:
error in Projectname setup command: 'extras_require' must be a dictionary whose values are strings or lists of strings containing valid project/version requirement specifiers.
相关包的版本:
pip 20.2.2
setuptools 50.0.0
如何修改我的setup.py
或我的需求文件来解决这个问题?
编辑
setup.py
在按照Martijn Pieters的anwser修改我之后,我可以确认现在将我的需求文件转换为一个列表,在需要的地方使用 name@url 直接引用语法。load_requirements
>>> load_requirements('./requirements/prod.txt')
['absl-py==0.8.1', 'GitPython==3.1.0', 'numpy==1.18.4', 'pip==20.2.2', 'protobuf==3.12.0', 'setuptools==41.0.0', 'scikit_learn==0.22', 'tensorflow_hub==0.8.0', 'importlib-metadata==1.6.1', 'keras-tuner==1.0.1', 'apache-beam==2.23.0', 'ml-metadata==0.23.0', 'pyarrow==0.17.0', 'tensorflow==2.3.0', 'tensorflow-data-validation==0.23.0', 'tensorflow-metadata==0.23.0', 'tensorflow-model-analysis==0.23.0', 'tensorflow-transform==0.23.0', 'tta_wrapper @ git://github.com/BioGeek/tta_wrapper.git@master']
但是,现在我在运行时收到以下错误python setup.py build
:
$ python setup.py build
/home/biogeek/code/programname/env/lib/python3.6/site-packages/_distutils_hack/__init__.py:30: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")
running build
Traceback (most recent call last):
File "setup.py", line 91, in <module>
install_requires=load_requirements('./requirements/prod.txt')
File "/home/biogeek/code/programname/env/lib/python3.6/site-packages/setuptools/__init__.py", line 153, in setup
return distutils.core.setup(**attrs)
File "/home/biogeek/code/programname/env/lib/python3.6/site-packages/setuptools/_distutils/core.py", line 148, in setup
dist.run_commands()
File "/home/biogeek/code/programname/env/lib/python3.6/site-packages/setuptools/_distutils/dist.py", line 967, in run_commands
self.run_command(cmd)
File "/home/biogeek/code/programname/env/lib/python3.6/site-packages/setuptools/_distutils/dist.py", line 984, in run_command
cmd_obj = self.get_command_obj(command)
File "/home/biogeek/code/programname/env/lib/python3.6/site-packages/setuptools/_distutils/dist.py", line 859, in get_command_obj
cmd_obj = self.command_obj[command] = klass(self)
File "/usr/lib/python3.6/distutils/cmd.py", line 57, in __init__
raise TypeError("dist must be a Distribution instance")
TypeError: dist must be a Distribution instance
编辑 2
我终于让我的安装成功了。我尝试了一些事情,所以不完全确定最终解决了这个问题,但我:
setuptools
从 降级50.0.0
为41.0.0
- 放在
setuptools
我的需求文件的第一行(见这里) - 添加了一个粗略的、hacky 的一次性函数,以使用名称 @ url 语法指向 zip 存档。
def _format_requirement(req):
if str(req.requirement) == 'git://github.com/BioGeek/tta_wrapper.git@master#egg=tta_wrapper':
return 'tta_wrapper @ https://github.com/BioGeek/tta_wrapper/archive/v0.0.1.zip'
return str(req.requirement)
解决方案
您只能使用PEP 508 - Python 软件包要求的依赖规范。git://github.com/BioGeek/tta_wrapper.git@master#egg=tta_wrapper
根据该标准,它不是有效的语法。
setuptools
确实接受name@ url
直接引用语法:
tta_wrapper @ git://github.com/BioGeek/tta_wrapper.git
但是,您不能将其放在 requirements.txt 文件中,也不能使用开关-e
。后者只能采用 VCS URL 或本地文件路径,而不是需求规范;请参阅需求文件格式部分。
因此,您可以在此处转换格式。我会检查产生的对象is_editable
上的标志,并相应地改变行为。您必须将需求字符串解析为 URL,拉出片段并将其放在前面:ParsedRequirement
parse_requirements()
#egg=
from urllib.parse import urlparse
def _format_requirement(req):
if req.is_editable:
# parse out egg=... fragment from VCS URL
parsed = urlparse(req.requirement)
egg_name = parsed.fragment.partition("egg=")[-1]
without_fragment = parsed._replace(fragment="").geturl()
return f"{egg_name} @ {without_fragment}"
return req.requirement
def load_requirements(fname):
"""Turn requirements.txt into a list"""
reqs = parse_requirements(fname, session="test")
return [_format_requirement(ir) for ir in reqs]
然后上面-e git:...#egg=tta_wrapper
变成tta_wrapper @ git:...
:
>>> load_requirements('./requirements/dev.txt')
['tta_wrapper @ git://github.com/BioGeek/tta_wrapper.git@master', 'black==20.08b1']
推荐阅读
- svn - svnsync 失败并显示“E175002: REPORT request on '/
/!svn/rev/71' 失败" - regex - 将某个关键字后面的单词转换为小写
- python - 每 6 个月显示一次 x 轴日期
- qt - 在目标上使用 Qt 5.3 QML 视频得到错误:“找不到资源”代码在主机系统上工作,两者都是 Linux
- emscripten - 带 -O3 的 emscripten 剥离函数
- android - 带有证书固定应用程序的中间人代理
- java - 奇怪地扫描 NFC 标签时出现 IOException
- python - 对于 python 中的字典列表和其他类型的列表,切片复制的工作方式是否不同?
- c# - 使用 MVVM Light 绑定嵌套属性
- swift - ARKit hitTest(_:options:) 选择放置的 3d 对象不起作用