setuptools setup.py 文件中 install_requires kwarg 的参考 requirements.txt [英] Reference requirements.txt for the install_requires kwarg in setuptools setup.py file

查看:31
本文介绍了setuptools setup.py 文件中 install_requires kwarg 的参考 requirements.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 Travis-CI 的 requirements.txt 文件.在 requirements.txtsetup.py 中复制需求似乎很愚蠢,所以我希望将文件句柄传递给 install_requiressetuptools.setup 中的 kwarg.

I have a requirements.txt file that I'm using with Travis-CI. It seems silly to duplicate the requirements in both requirements.txt and setup.py, so I was hoping to pass a file handle to the install_requires kwarg in setuptools.setup.

这可能吗?如果是这样,我应该怎么做?

Is this possible? If so, how should I go about doing it?

这是我的 requirements.txt 文件:

guessit>=0.5.2
tvdb_api>=1.8.2
hachoir-metadata>=1.3.3
hachoir-core>=1.3.3
hachoir-parser>=1.3.4

推荐答案

您可以翻转它并在 setup.py 中列出依赖项,并使用单个字符 —一个点 . —在 requirements.txt 中.

You can flip it around and list the dependencies in setup.py and have a single character — a dot . — in requirements.txt instead.

或者,即使不建议,仍然可以使用以下 hack 解析 requirements.txt 文件(如果它没有通过 URL 引用任何外部需求)>pip 9.0.1):

Alternatively, even if not advised, it is still possible to parse the requirements.txt file (if it doesn't refer any external requirements by URL) with the following hack (tested with pip 9.0.1):

install_reqs = parse_requirements('requirements.txt', session='hack')

这不会过滤环境标记.

在旧版本的 pip,更具体地说,6.0 之前,有一个公共可用于实现此目的的 API.需求文件可以包含注释(#),也可以包含一些其他文件(--requirement-r).因此,如果你真的想解析一个 requirements.txt 你可以使用 pip 解析器:

In old versions of pip, more specifically older than 6.0, there is a public API that can be used to achieve this. A requirement file can contain comments (#) and can include some other files (--requirement or -r). Thus, if you really want to parse a requirements.txt you can use the pip parser:

from pip.req import parse_requirements

# parse_requirements() returns generator of pip.req.InstallRequirement objects
install_reqs = parse_requirements(<requirements_path>)

# reqs is a list of requirement
# e.g. ['django==1.5.1', 'mezzanine==1.4.6']
reqs = [str(ir.req) for ir in install_reqs]

setup(
    ...
    install_requires=reqs
)

这篇关于setuptools setup.py 文件中 install_requires kwarg 的参考 requirements.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆