How To Generate The Requirements Of Your Python Project Based On Your Imports

How to add only the imported libraries to the requirements.txt files

George Pipis
2 min readOct 18, 2021


Image on Unsplash

We have provided many tutorials on how to generate the requirements.txt for your python project without environments, how to work with Conda environments, how to work with VS Code and Virtual Environments and so on. Today, we will provide an alternative way to get the requirement.txt file by including only the library that we have used, in other words, only the libraries that we have imported. We will provide two approaches, the first one is when we work with .py files and the second one when we work with Jupyter notebooks.

Working with .py files

Let’s assume that we work on the project, called “pipreqs_example“, where there is our .py file containing the code of the project. In order to make it reproducible, we would like to generate the “requirements.txt” file but ONLY for the used libraries. We can easily achieve that by using the pipreqs library. We can install the library as follows:

pip install pipreqs

Within the project I have a .py file with the following imports:

import numpy as np
import pandas as pd
import re

Let’s see how we can generate the requirements.txt file. We can either specify the whole path of the project or run the following command within the project path.

pipreqs --force

And the requirements.txt appears in the project directory!


Working with Jupyter notebooks

If you work with Jupyter notebooks, you can use the pipreqsnb library. You can install the library as follows:

pip install pipreqsnb

We work similarly as before, but not the command is:

pipreqsnb --force

Note that pipreqsnb is a very simple fully compatible pipreqs wrapper that supports python files and jupyter notebooks.

Originally posted at Predictive Hacks



George Pipis

Sr. Director, Data Scientist @ Persado | Co-founder of the Data Science blog: