Watch on YouTube
Why do I need a requirements.txt
file?
The requirements.txt
has two major benefits.
-
It tells other developers which libraries your application needs.
-
Instead of installing libraries one by one, you can install them all at once with:
# bash
$ pip install -r requirements.txt
Tutorial
- Create a new project folder
For this tutorial, we’ll call it python_requirements, but you can name it anything.
- Change into the new project directory, install a new virutal environment, and activate it.
# bash
$ cd python_requirements
$ python -m venv venv
# Mac and Linux
$ source venv/bin/activate
# Windows users run a different command
$ .\bubble\Scripts\activate
- Install the following libraries using pip.
# bash
$ pip install pyjokes
$ pip install quoters
$ pip install random_word
- Create a new app called
app.py
and import the three libraries into the app.
I’m going to use nano for this example but you could easily use an IDE like VS Code.
# bash
$ nano app.py
# python
from quoters import Quote
from random_word import RandomWords
import pyjokes
print("hello")
Create requirments.txt
the standard way
- In a terminal within the project/app directory run the command:
# bash
pip freeze > requirements.txt
This command will add all the libraries to the requirements.txt. From the imported libraries used to the dependencies, etc.
Only used libraries requirements.txt
(alternative)
- Install the library
pipreqs
# bash
$ pip install pipreqs
- The run the command:
# bash
# if you are in the project directory, then just run pipreqs
$ pipreqs /path/to/your/project
# for this project, I was in the project directory so I ran:
$ pipreqs
- Running this command will generate the
requirements.txt
with only the libraries imported / used in the project.
# bash
pyjokes==0.8.3
quoters==0.30
random_word==1.0.13
🎉 Congrats
You now know how to generate a requirements.txt
which will help with future personal and team projects.