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 wraps virtualenv
producing some improvements over the version specification that is created by pipreqs
.
-- Edit 2:
If, after creating the file with the dependency libraries of your module with pipreqs
, you want to pin the whole dependency tree, take a look at pip-compile
. It figures out a way to get the dependencies of your top level libraries, and it pins them in a new requirement files, indicating the dependency tree.
-- Edit 3:
If you want to split your dependency tree 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
.
-- Edit 4:
At this point in time, I would recommend taking a look at poetry/PDM/Hatch and start projects directly with this dependency manager. Dependency tree comes out of the box.
pip freeze
as I guess that pip freeze will simply list down all the packages present in that virtual env , but that might not be used by my application and I might have messed up my env by adding some unwanted packages.. how to solve that? I am more interested in cleaning up the dependencies list, sort of!! – Wallaroo