If we check out some online Python projects on GitHub, chances are that they have a requirements.txt text file in the project. This requirements.txt contains the Python libraries and their specific versions that we need to install before we can run the project. In this article, let’s run through the steps we need to take to generate a requirements.txt for our Python project.
Understanding pip freeze
The first command we need to understand would be pip freeze. Note that we type this into our terminal (macOS) or Cmd (Windows)
We might get something like this:
# and a whole lot of other stuff
# mine has 110 lines in total
And if we add > requirements.txt to the back:
pip freeze > requirements.txt
These packages will be written to a text file requirements.txt instead of being printed out on the terminal. The stuff that is printed out is essentially every package that you’ve installed using pip on your computer, along with their versions. This list will probably contain the packages you need for your project, but it will likely contain a whole bunch of other stuff that you don’t need.
As we probably don’t want to tell our users to install all these irrelevant stuff, we need a way to make pip freeze > requirements.txt write only the relevant packages to requirements.txt. Or else our users might be pissed at us for asking them to install a bunch of unneeded stuff. We can achieve this using a Python virtual environment.
Understanding A Python Virtual Environment
A virtual environment is a separate Python environment where the Python interpreter, libraries, and scripts installed inside are isolated from other environments. You can think of it as an additional disposable Python installation on your computer that we can turn on and off at will, and which contains its packages. If we screw up your virtual environment, we can simply delete it and create a new one.
Creating A Virtual Environment Using Venv
To create a virtual environment, we can use the Python venv library. Type this into your terminal or cmd:
python -m venv env
Note: macOS users, remember to replace python with python3
The -m flag tells Python that we want to use a library and the venv is simply the library that we want to use. env here refers to the name of the environment that we will create. After running this command, our working directory should have a new env folder. This is our virtual environment.
Activating our Virtual Environment
We need to do this in the terminal/cmd in the same directory as our virtual environment folder.
Remember to change env to your environment’s name if you didn’t use env
You should see something like this afterward — this means that you are already in your Python virtual environment. Our next step would be to install only the relevant Python libraries in our virtual environment.
Installing Relevant Python Libraries Using Pip
Let’s say I’m working on a Python FastAPI project that uses pandas somewhere in my code, and the only things I need to install would be fastapi, uvicorn and pandas. I simply need to install these libraries inside my virtual environment using pip:
pip install fastapi uvicorn pandas
My sample code (app.py):
import pandas as pd
from fastapi import FastAPI
app = FastAPI()
"message": "hello world"
# other stuff
if __name__ == "__main__":
uvicorn.run("app:app", host="127.0.0.1", port=5000)
Generate Requirements.txt Using pip freeze
Once we’ve installed only the relevant packages and libraries for our project and verified that our project works fine, we can move on to the final step — generating requirements.txt using the pip freeze > requirements.txt command as discussed earlier.
pip freeze > requirements.txt
And we get this when we check requirements.txt:
There are only 18 packages here as opposed to 100+ at the start of the article, and none of these 18 packages are irrelevant packages that are not used by our project. There, our requirements.txt is ready to get uploaded to GitHub!
Deactivating our Virtual Environment
To deactivate our virtual environment, we simply need to type deactivate into our terminal/cmd