9

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Kimmi
  • 591
  • 2
  • 7
  • 22
  • I would say use `virtual environment` on you development machine, and then just do `pip freeze` – Vor Jun 09 '14 at 15:34
  • I still got a bit confused, would you mind to provide an example or instructions? – Kimmi Jun 09 '14 at 15:42

2 Answers2

8

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
Daniel Braun
  • 2,452
  • 27
  • 25
  • 1
    Thanks for this. Just keep in mind some packages might get named incorrectly so need to review it (eg. `scikit-image==0.17.2` will become `skimage==0.0`) – Justas Oct 07 '20 at 07:32
  • Link to pipreqs: https://github.com/bndr/pipreqs – ksgj1 May 04 '22 at 07:36
7

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)
    
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195