List dependencies in Python
What is the most efficient way to list all dependencies required to deploy a working project elsewhere (on a different OS, say)?
Python 2.7, Windows dev environment, not using a virtualenv per project, but a global dev environment, installing libraries as needed, happily hopping from one project to the next.
I've kept track of most (not sure all) libraries I had to install for a given project. I have not kept track of any sub-dependencies that came auto-installed with them. Doing pip freeze
lists both, plus all the other libraries that were ever installed.
Is there a way to list what you need to install, no more, no less, to deploy the project?
EDIT In view of the answers below, some clarification. My project consists of a bunch of modules (that I wrote), each with a bunch of import
s. Should I just copy-paste all the imports from all modules into a single file, sort eliminating duplicates, and throw out all from the standard library (and how do I know they are)? Or is there a better way? That's the question.
pipreqs
solves the problem. It generates project-level requirement.txt file.
Install pipreqs: pip install pipreqs
- Generate project-level requirement.txt file:
pipreqs /path/to/your/project/
- requirements file would be saved in /path/to/your/project/requirements.txt
If you want to read more advantages of pipreqs
over pip freeze
, read it from here
Scan your import
statements. Chances are you only import things you explicitly wanted to import, and not the dependencies.
Make a list like the one pip freeze
does, then create and activate a virtualenv.
Do pip install -r your_list
, and try to run your code in that virtualenv. Heed any ImportError
exceptions, match them to packages, and add to your list. Repeat until your code runs without problems.
Now you have a list to feed to pip install
on your deployment site.
This is extremely manual, but requires no external tools, and forces you to make sure that your code runs. (Running your test suite as a check is great but not sufficient.)
On your terminal type:
pip install pipdeptree
cd <your project root>
pipdeptree