[Django]-Virtualenv – Cleaning up unused package installations

3πŸ‘

βœ…

A method I use is with my requirements.txt files. From the root of my Django project, I create a requirements/ directory with the following files in it:

requirements/
    base.txt
    dev.txt
    prod.txt
    temp.txt

base.txt contains packages to be used in all environments such as Django==1.8.6.

Then dev would include base and other packages, and might look like:

-r base.txt
coverage==4.0.2

Then temp.txt includes dev.txt and contains packages I’m not sure I’ll use permanently:

-r dev.txt
temp_package==1.0
git+https://github.com/django/django.git#1014ba026e879e56e0f265a8d9f54e6f39843348

Then I can blow away the entire virtualenv and reinstall it from the appropriate requirements file like so:

pip install -r requirements/dev.txt

Or, to include the temp_package I’m testing:

pip install -r requirements/temp.txt

That’s just how I do it, and it helps keep my sandbox separate from the finished product.

πŸ‘€FlipperPA

0πŸ‘

Potentially, you could use isort and run isort myproject/* --diff to get a list of proposed changes isort would make to your project.

In the proposed changes, it list imports that are not used. From that, you could take a look at packages installed in your virtual environment and start removing them with pip. This is assuming you did not remove the import statements, which may not be the case.

Another way would be to create a new env and attempt to run your app. Use error messages to get the packages you need until your app runs. Not pretty, but it would work.

πŸ‘€knelson

Leave a comment