Is there any way to automatic detect required modules and packages in my own python 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)
    

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