blz blz - 6 months ago 65
Python Question

How can I reference requirements.txt for the install_requires kwarg in setuptools' setup.py file?

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?

For good measure, here is my
requirements.txt
file:

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

Answer

A requirement file can contain comments (#) and can include some other files (--requirement or -r). Thus, if you really want to parse a requirement.txt you should 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>, session=False)

# 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
)
Comments