Sometimes I download the python source code from github
and don't know how to install all the dependencies. If there is no requirements.txt
file I have to create it by hands.
The question is:
Given the python source code directory is it possible to create requirements.txt
automatically from the import section?
You can use the following code to generate a requirements.txt file:
pip install pipreqs
pipreqs /path/to/project
more info related to pipreqs can be found here.
Sometimes you come across pip freeze
, but this saves all packages in the environment including those that you don't use in your current project.
In my case, I use Anaconda, so running the following command from conda terminal inside my environment solved it, and created this requirements txt file for me automatically:
conda list -e > requirements.txt
This was taken from this Github link pratos/condaenv.txt
Make sure to run pip3 for python3.7.
pip3 freeze >> yourfile.txt
Before executing the above command make sure you have created a virtual environment.
python3:
pip3 install virtualenv
python3 -m venv <myenvname>
python2:
pip install virtualenv
virtualenv <myenvname>
After that put your source code in the directory. If you run the python file now, probably It won't launch If you are using non-native modules. You can install those modules runing
pip3 install <module> or pip install <module>
This will not affect you entire module list except the environment you are In.
Now you can execute the command at the top and now you have a requirements file which contains only the modules you installed in the virtual environment. Now you can run the command at the top.
I advise everyone to use environments as It makes things easier when It comes to stuff like this.
Hope this helped.
来源:https://stackoverflow.com/questions/31684375/automatically-create-requirements-txt