I have a simple code that runs on a VPS and I want to know how I can check if the required modules are installed when my script is run. I also need to find a way to generate a list of modules the script uses when run on my local machine in-order to use it in the VPS. I've read many questions and answers but they all are linked to many complex methods that I can't understand at all.
-
Does this answer your questions: https://stackoverflow.com/questions/31684375/automatically-create-requirements-txt – Mark Dec 12 '20 at 04:18
-
Refer this link I believe this will be easy to understand as well http://blog.rtwilson.com/how-to-find-out-what-modules-a-python-script-requires/ – Sachin Rajput Dec 12 '20 at 04:19
-
@Mark Meyer is there a way to get the modules that are only used by the script? `pip > freeze` created a list of all my modules – Yas Dec 12 '20 at 04:21
2 Answers
Method 1:
The post here will explain how you can export a list of packages - regardless of whether they are used in your project or not. You could then manually filter through and remove the redundant ones yourself.
Method 2:
You can use the pip-check-reqs package to check for missing dependencies not currently in your requirements.txt
file -- which you can then add to it manually. Unfortunately, it currently doesn't output version information.
Method 3:
Combine the above two methods together. Write a script that will use the requirements list generated from the first method, and filter it according to those generated using the second method.
Method 4 (Legacy):
There used to be a great little package called pipreqs. It would generate a suitable requirements.txt
file for you automatically. Unfortunately, it broke sometime around or after Python 3.6. I'm currently not aware of any alternative package.

- 5,047
- 11
- 16
- 26
One simple method to generate a requirements.txt file is to pipe them,
pip freeze > requirements.txt
Make sure you are in the directory where you need to create the requirements.txt file and use the above-mentioned command in the terminal and it will do the rest for you.

- 534
- 5
- 14