views:

83

answers:

2

How can I specify optional dependencies in a pip requirements file? According to the pip documentation this is possible, but the documentation doesn't explain how to do it and I can't find any examples on the web.

+1  A: 

Instead of specifying optional dependencies in the same file as the hard requirements, you can create a optional-requirements.txt and a requirements.txt.

To export your current environment's packages into a text file, you can do this:

pip freeze > requirements.txt

If necessary, modify the contents of the requirements.txt to accurately represent your project's dependencies. Then, to install all the packages in this file, run:

pip install -U -r requirements.txt

-U tells pip to upgrade packages to the latest version, and -r tells it to install all packages in requirements.txt.

Daniel
I think you've misunderstood the question. 'pip freeze' will just print out all of the dependencies. What I want to know is how I can specify which dependencies are required and which are optional within the pip requirements file.
del
I see the reference in the docs I think you're referring to, but I'm not sure it's possible in one requirements file... although you could have two dependency files, one which lists optional dependencies. I'll modify my answer
Daniel
Thanks - this is the approach I was already taking, but reading the bit about optional dependencies in the doc made me think there might be a better way to do it.
del
A: 

You are misunderstanding the docs; they are not as clear as they could be. The point in the docs is that with a requirements file you can feel free to specify your full recommended working set of packages, including both necessary dependencies and optional ones. You can add comments (lines beginning with #) to distinguish the two to humans, but pip makes no distinction. You can also have two requirements files, as Daniel suggests.

Carl Meyer
You aren't really free to include both the required and optional dependencies in a requirements file because 'pip install' will immediately abort if any of the packages in the file fail to install. It seems that using two separate requirements files is the only correct solution.
del