How to Generate Requirements.txt for Your Python Project

A guide to generating Requirements.txt for Python project.

image

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)

pip freeze

We might get something like this:

anyio==2.1.0
appnope==0.1.2
argon2-cffi==20.1.0
async-generator==1.10
attrs==20.3.0
Babel==2.9.0
backcall==0.2.0
bleach==3.3.0
blis==0.4.1
catalogue==1.0.0
certifi==2020.12.5
# 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.

Windows

env/bin/activate.bat

For macOS

source env/bin/activate

Remember to change env to your environment’s name if you didn’t use env

image

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
import uvicorn

app = FastAPI()

@app.get("/")
def home():
    return {
        "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:

anyio==3.4.0
asgiref==3.4.1
click==8.0.3
fastapi==0.70.0
h11==0.12.0
idna==3.3
importlib-metadata==4.8.2
numpy==1.21.4
pandas==1.3.4
pydantic==1.8.2
python-dateutil==2.8.2
pytz==2021.3
six==1.16.0
sniffio==1.2.0
starlette==0.16.0
typing-extensions==4.0.0
uvicorn==0.15.0
zipp==3.6.0

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

deactivate

To deactivate our virtual environment, we simply need to type deactivate into our terminal/cmd

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics