I have a requirements file which contains all installed packages. After big refactoring process of the project some of the listed packages are not needed anymore. The problem is I'm not sure which. Is there a way to determine which packages listed in the requirements.txt file are actually used in the code?
-
Do you have automated tests that you can run, ideally with high coverage? The brute force approach is to start with an empty venv, run your tests, and install the packages for each modulenotfounderror,.. until all the tests pass. – Gino Mempin Oct 11 '21 at 11:32
-
`$ cat **/*.py | grep import` could be a starting point? – aviya.developer Oct 11 '21 at 11:35
-
@GinoMempin actually my project is a big cutsom build test automation framework for some product, and it does not have any additional tests. – Marcin Oct 11 '21 at 11:38
-
@aviya.developer so you;re suggestion is the to write additional script that will grep all of the imported packages? I will probably use this as a last resort. For now I'm thinking of more elegant solution. – Marcin Oct 11 '21 at 11:41
-
1`python -v …` Take notes, be fast! ;) – Klaus D. Oct 11 '21 at 11:56
2 Answers
Alternative answer that uses a Python library: pipreqs
. See Automatically create requirements.txt.
Running pipreqs with the default arguments will generate a requirements.txt
for you.
$ pipreqs /home/project/location
Successfully saved requirements file in /home/project/location/requirements.txt
However, it looks like you're trying to clean up an old requirements.txt
file. In this case, pipreqs
also comes with a --diff
flag, and a --clean
flag. From the docs:
--diff <file> Compare modules in requirements.txt to project imports.
--clean <file> Clean up requirements.txt by removing modules that are not imported in project.
You can use --diff
to determine which libraries need to be removed, and --clean
to do it automagically.

- 2,010
- 1
- 15
- 32
If you are using a virtual environment and have a decent enough test suite that you can repeatedly and automatically run (it's best if you can run it on your local workspace via a script or a simple command), then the brute-force type of approach is to:
- Setup a fresh copy of your project (ex.
git clone
onto a separate folder) - Setup an empty/blank virtual environment
- Run your tests
- Install missing packages every time you encounter a "ModuleNotFoundError" type of error
- Repeat until all tests passes
- Export the packages you now have to a separate requirements.txt file (
pip freeze > requirements.new.txt
or any of the other ways from Automatically create requirements.txt)
For example, we have this (very) minimal example code:
# --- APP CODE
from numpy import array
from openpyxl import Workbook
from pydantic import BaseModel, ValidationError
class MyModel(BaseModel):
x: int
# --- TEST CODE
import pytest
def test_my_model():
with pytest.raises(ValidationError):
MyModel(x="not an int")
After cloning this and setting up a brand new fresh virtual environment (but without yet installing any of the packages), the 1st attempt to run the tests yields:
(my_venv) $ pytest --maxfail=1 main.py
bash: /Users/me/.venvs/my-proj-7_w3b8eb/bin/pytest: No such file or directory
So then, you install pytest.
Then, you get:
(my_venv) $ pytest --maxfail=1 main.py
...
main.py:1: in <module>
from numpy import array
E ModuleNotFoundError: No module named 'numpy'
So then you install numpy.
Then, you get:
(my_venv) $ pytest --maxfail=1 main.py
...
main.py:2: in <module>
from openpyxl import Workbook
E ModuleNotFoundError: No module named 'openpyxl'
So then you install openpyxl.
...and so on. Until all the tests passes. Of course, even when your automated tests pass, it's good to also do some manual basic tests to make sure everything is indeed working as before. Finally, generate a new copy of your requirements.txt file, and compare that with the old one, to check for any differences.
Of course, as I mentioned at the start, this assumes you have a decent enough test suite that tests a large % of your code and use cases. (It's also one of the good reasons why you should be writing tests in the first place.).

- 25,369
- 29
- 96
- 135