I want to create a python distribution package, and need to check all the required packages for my own package.
For requirements.txt, it include all the packages it needs. However, I want to find a way to check all the packages I need, and also, for some of the packages, they are also some requirements of other packages in my project.
Is there any way to check which packages I need, and maintain the minimum required packages for my own project?
pip freeze
will print whatever packages happended to be installed in your current envirenment. To list the packages that are actually being imported use pipreqs
:
pip install pipreqs
pipreqs path_to_project
Usually people know their requirements by having separate virtual environments with required modules installed. In this case, it is trivial to make the requirements.txt
file by running the following while being inside the virtual environment:
pip freeze > requirements.txt
Also, to avoid surprises in production and be confident about the code you have, would be good to have tests and a good test coverage. In case, there is a module imported but not installed, tests would show it.
Another way to find modules that cannot be imported is by using pylint
static code analysis tool against the package. There is a special F0401 - Unable to import %s
warning.
Demo:
imagine you have a test.py
file that has a single import
statement
import pandas
pandas
module is not installed in the current python environment
here is the output of pylint test.py
:
$ pylint test.py
No config file found, using default configuration
************* Module test
C: 1, 0: Missing module docstring (missing-docstring)
F: 1, 0: Unable to import 'pandas' (import-error)
W: 1, 0: Unused import pandas (unused-import)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With