问题描述
我有一个与Travis-CI一起使用的requirements.txt
文件.复制requirements.txt
和setup.py
中的要求似乎很愚蠢,所以我希望将文件句柄传递给setuptools.setup
中的install_requires
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.
或者,即使不建议使用,也可以通过以下技巧(已通过pip 9.0.1
测试)来解析requirements.txt
文件(如果未通过URL引用任何外部要求):
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!