The Python Oracle

Automatically create requirements.txt

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Civilization

--

Chapters
00:00 Automatically Create Requirements.Txt
00:24 Answer 1 Score 1559
00:58 Accepted Answer Score 1388
01:15 Answer 3 Score 55
01:51 Answer 4 Score 154
02:12 Thank you

--

Full question
https://stackoverflow.com/questions/3168...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #dependencies #pythonimport #requirementstxt

#avk47



ANSWER 1

Score 1559


You can use the following code to generate a requirements.txt file:

pip install pipreqs
pipreqs /path/to/project

The benefits of using pipreqs from its GitHub.

Why not pip freeze?

  • pip freeze only saves the packages that are installed with pip install in your environment.
  • pip freeze saves all packages in the environment including those that you don't use in your current project (if you don't have virtualenv).
  • and sometimes you just need to create requirements.txt for a new project without installing modules.



ACCEPTED ANSWER

Score 1388


Use Pipenv or other tools is recommended for improving your development flow.

pip3 freeze > requirements.txt  # Python3
pip freeze > requirements.txt  # Python2

If you do not use a virtual environment, pigar will be a good choice for you.




ANSWER 3

Score 154


For python3: (I have both python 2 and 3 on my machine, where python2 is the default)

# install
pip3 install pipreqs

# Run in current directory
python3 -m  pipreqs.pipreqs .

python2:

pip install pipreqs
python -m  pipreqs.pipreqs .

To check your python version:

python --version



ANSWER 4

Score 55


In my case, I use Anaconda, so running the following command from a 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.

If an error have been seen, and you are using Anaconda, try to use the .yml option:

conda env export > <environment-name>.yml

For another person to use the environment or if you are creating a new environment on another machine:

conda env create -f <environment-name>.yml

.yml option been found here