Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to automatic detect required modules and packages in my own python project

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?

like image 518
Kimmi Avatar asked Jun 09 '14 15:06

Kimmi


2 Answers

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
like image 59
Daniel Braun Avatar answered Oct 12 '22 20:10

Daniel Braun


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)
    
like image 32
alecxe Avatar answered Oct 12 '22 22:10

alecxe