Harshit verma
Harshit verma

Reputation: 548

Remove redundancy in requirements.txt in Django projects

I wonder if there is a tool by which we can figure out what packages are not in used in our requirements.txt because I have tons of dependencies written in the requirements.txt but I am sure a lot of them are not in use but at the same time I am not sure as well.

Looking for some help via tools or any conventional method if thats possible.

Thanks!

Upvotes: 2

Views: 1756

Answers (2)

Oliver
Oliver

Reputation: 29571

pipreqs --force will determine all your requirements and write them to requirements.txt. Since this will only list requirements derived from your code, this will achieve the desired outcome.

Upvotes: 0

Ralf Zosel
Ralf Zosel

Reputation: 693

I also had tons of unused packages (until I learned how to use virtualenv properly). My requirements.txt was way too long. For me, pipreqs helped to reduce it to the packages I really need:

  1. Go to your project folder and remove the virtual environment (if existant): rm -r venv
  2. Install pipreqs: pip install pipreqs
  3. Run pipreqs: pipreqs --force
  4. Install new virtual environment: python3 -m venv venv
  5. Activate it: source venv/bin/activate
  6. Install required modules: pip install -r requirements.txt
  7. Build the complete list which includes the dependencies: pip freeze > requirements.txt

Caveat: I am not an experienced programmer, I found this solution by myself and I did not check it extensively (so far), but a quick check shows that my Django project is still working.

Upvotes: 2

Related Questions