The Python Oracle

Automatically create requirements.txt

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life

--

Chapters
00:00 Question
00:32 Accepted answer (Score 1034)
00:53 Answer 2 (Score 1118)
01:20 Answer 3 (Score 99)
01:52 Answer 4 (Score 50)
02:47 Thank you

--

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

Accepted answer links:
[Pipenv]: https://packaging.python.org/tutorials/m...
[pigar]: https://github.com/Damnever/pigar

Answer 2 links:
[here]: https://github.com/bndr/pipreqs

Answer 4 links:
[pratos/condaenv.txt]: https://gist.github.com/pratos/e167d4b00...
[.yml option been found here]: https://gist.github.com/pratos/e167d4b00...

--

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