22

I would like to know how to extract the requirements from a single python script. I tried the following way, at the beginning of my file, immediately after the imports:

try:
    from pip._internal.operations import freeze
except ImportError:  # pip < 10.0
    from pip.operations import freeze

x = freeze.freeze()
for p in x:
    print(p)

The piece of code above, however, gives me back all the Python frameworks installed locally. I would like to extract only the necessary requirements for the script, in order to be able to deploying the final application.
I hope I was clear.

Memmo
  • 298
  • 3
  • 8
  • 31
  • 2
    There is no way for pip to know this. The only way to know what the requirements for a module are is for someone to document them. – Daniel Roseman Feb 13 '19 at 13:10
  • And additionally, this sounds a bit like an XY problem. What's the actual problem you're trying to solve? – grooveplex Feb 13 '19 at 13:12
  • 3
    Create a virtual environment, install only the necessary requirements for the script, and then pip freeze will give you only what you need. – BitParser Feb 13 '19 at 13:12
  • @grooveplex In a nutshell, I would like to extract the requirements of a python file, but not all the libraries installed locally!! Which the piece of code mentioned above gives me back ... – Memmo Feb 13 '19 at 13:17
  • @TGO Quite right! I had not thought about it... This could be a good idea, thank you! – Memmo Feb 13 '19 at 13:18
  • 1
    The developer is responsible for the installed packages and a clean `requirements.txt`. At work we maintain the packages in the `requirements.txt` by hand, because a `pip freeze` adds all dependencies and sub-dependencies to the list which is sometimes confusing. Nevertheless you can checkout [modulefinder](https://docs.python.org/3.7/library/modulefinder.html) which is a stdlib library to inspect the loaded modules. – Darius Feb 13 '19 at 13:26
  • @DariusMorawiec another good and pythonic solution! Thanks, I really like this! – Memmo Feb 13 '19 at 13:33

2 Answers2

33

pipreqs is simple to use

install:

pip install pipreqs

in linux in the same folder of your script use:

pipreqs .

then the requirements.txt file is created

pip home page:

https://pypi.org/project/pipreqs/

Jose R. Zapata
  • 709
  • 6
  • 13
5

You can do this easily with 'modulefinder' python module.

I think you want to print all the modules required by a script. So, you can refer to

http://blog.rtwilson.com/how-to-find-out-what-modules-a-python-script-requires/

or for your ease the code is here:


from modulefinder import ModuleFinder
f = ModuleFinder()
# Run the main script
f.run_script('run.py')
# Get names of all the imported modules
names = list(f.modules.keys())
# Get a sorted list of the root modules imported
basemods = sorted(set([name.split('.')[0] for name in names]))
# Print it nicely
print ("\n".join(basemods))

Kumar Saptam
  • 336
  • 5
  • 18