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?
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
Requirements files use an expanded pip format, which is only useful if you need to complement your
setup.py
with stronger constraints, for example specifying the exact urls some of the dependencies must come from, or the output ofpip freeze
to freeze the entire package set to known-working versions. If you don't need the extra constraints, use only asetup.py
. If you feel like you really need to ship arequirements.txt
anyway, you can make it a single line:It will be valid and refer exactly to the contents of the
setup.py
that is in the same directory.from pip.req import parse_requirements
did not work for me and I think it's for the blank lines in my requirements.txt, but this function does workMost of the other answers above don't work with the current version of pip's API. Here is the correct* way to do it with the current version of pip (6.0.8 at the time of writing, also worked in 7.1.2. You can check your version with pip -V).
* Correct, in that it is the way to use parse_requirements with the current pip. It still probably isn't the best way to do it, since, as posters above said, pip doesn't really maintain an API.
Yet another
parse_requirements
hack that also parses environment markers intoextras_require
:It should support both sdist and binary dists.
As stated by others,
parse_requirements
has several shortcomings, so this is not what you should do on public projects, but it may suffice for internal/personal projects.It can't take a file handle. The
install_requires
argument can only be a string or a list of strings.You can, of course, read your file in the setup script and pass it as a list of strings to
install_requires
.Here is a complete hack (tested with
pip 9.0.1
) based on Romain's answer that parsesrequirements.txt
and filters it according to current environment markers: