首页 > 解决方案 > '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 Pietersanwser修改我之后,我可以确认现在将我的需求文件转换为一个列表,在需要的地方使用 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

我终于让我的安装成功了。我尝试了一些事情,所以不完全确定最终解决了这个问题,但我:

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)

标签: python-3.xinstallationsetuptoolssetup.py

解决方案


您只能使用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,拉出片段并将其放在前面:ParsedRequirementparse_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']

推荐阅读