Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all modules/packages used by a python project

I have a python GUI application. And now I need to know what all libraries the application links to. So that I can check the license compatibility of all the libraries.

I have tried using strace, but strace seems to report all the packages even if they are not used by the application.

And, I tried python ModuleFinder but it just returns the modules that are inside python2.7 and not system level packages that are linked.

So is there any way I can get all the libraries that are linked from my application?

like image 789
user2109788 Avatar asked Mar 04 '16 13:03

user2109788


People also ask

How do I get all Python packages?

The Pip, Pipenv, Anaconda Navigator, and Conda Package Managers can all be used to list installed Python packages. You can also use the ActiveState Platform's command line interface (CLI), the State Tool to list all installed packages using a simple “state packages” command.

How do I find out how many Python modules I have?

In the standard Python interpreter, you can type " help('modules') ". At the command-line, you can use pydoc modules . In a script, call pkgutil.

How can I get a list of installed modules?

To see all modules installed on the system, use the Get-Module -ListAvailable command.

How do I list all packages in pip?

To do so, we can use the pip list -o or pip list --outdated command, which returns a list of packages with the version currently installed and the latest available. On the other hand, to list out all the packages that are up to date, we can use the pip list -u or pip list --uptodate command.


1 Answers

You can give a try to the library https://github.com/bndr/pipreqs found following the guide https://www.fullstackpython.com/application-dependencies.html


The library pipreqs is pip installable and automatically generates the file requirements.txt.

It contains all the imports libraries with versions you are using in the virtualenv or in the python correctly installed. Just type:

pip install pipreqs pipreqs /home/project/location 

It will print:

INFO: Successfully saved requirements file in /home/project/location/requirements.txt 

In addition it is compatible with the pip install -r command: if you need to create a venv of your project, or update your current python version with compatible libraries, you just need to type:

pip install -r requirements.txt 

I had the same problem and this library solved it for me. Not sure if it works for multiple layers of dependencies i.e. in case you have nested level of dependent libraries.

-- Edit 1:

If looking for a more sophisticated version manager, please consider as well pyvenv https://github.com/pyenv/pyenv. It blends virtualenv and pipreqs in the same tool, with some improvements over the version specification of pipreqs.

-- Edit 2:

If you want to split your library dependencies into different files (e.g. base, test, dev, docs) and have a way of managing the dependency tree, please take a look at pip-compile-multi.

like image 120
SeF Avatar answered Sep 18 '22 15:09

SeF