Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List dependencies in Python

Tags:

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 imports. 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.

like image 734
RolfBly Avatar asked Feb 14 '17 22:02

RolfBly


People also ask

How do I see all dependencies in Python?

Pip Check Command – Check Python Dependencies After Installation. Because pip doesn't currently address dependency issues on installation, the pip check command option can be used to verify that dependencies have been installed properly in your project. For example: $ pip check No broken requirements found.

What are the dependencies in Python?

Dependencies are all of the software components required by your project in order for it to work as intended and avoid runtime errors. You can count on PyPI (the Python Package Index) to provide packages that can help you get started on everything from data manipulation to machine learning to web development, and more.

Where are Python dependencies stored?

When a package is installed globally, it's made available to all users that log into the system. Typically, that means Python and all packages will get installed to a directory under /usr/local/bin/ for a Unix-based system, or \Program Files\ for Windows.

How do I see the dependency tree in pip?

You can do it by installing pipdeptree package. Open command prompt in your project folder. If you are using any virtual environment, then switch to that virtual environment. This package will list all the dependencies of your project.


3 Answers

pipreqs solves the problem. It generates project-level requirement.txt file.

Install pipreqs: pip install pipreqs

  1. Generate project-level requirement.txt file: pipreqs /path/to/your/project/
  2. 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

like image 141
Haifeng Zhang Avatar answered Oct 19 '22 11:10

Haifeng Zhang


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.)

like image 27
9000 Avatar answered Oct 19 '22 13:10

9000


On your terminal type:

pip install pipdeptree
cd <your project root>
pipdeptree
like image 12
Venu Gopal Tewari Avatar answered Oct 19 '22 12:10

Venu Gopal Tewari