Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete unused packages from requirements file

Is there any easy way to delete no-more-using packages from requirements file?

I wrote a bash script for this task but, it doesn't work as I expected. Because, some packages are not used following their PyPI project names. For example;

dj-database-url 

package is used as

dj_database_url 

My project has many packages in its own requirements file, so, searching them one-by-one is too messy, error-prone and takes too much time. As I searched, IDEs don't have this property, yet.

like image 557
myildirim Avatar asked Aug 19 '14 05:08

myildirim


People also ask

How do I remove unused packages from requirements txt?

txt, you can use the "Sync Python Requirements" tool (under the Tools menu) to find and remove unused packages (check the "Remove unused requirements" box). This could be a nightmare if you "inspect" a project and contain the whole dependencies inside of it, so delete the dependencies (if you have a virtual env.

How do I find unused dependencies in Python?

3 Answers. Show activity on this post. Inside Pycharm Go to code > inspect code Select Whole project option and click OK. In inspection results panel locate Package requirements section under Python (note that this section will be showed only if there is any requirements.

How do I remove unused packages in Linux?

If you want to retain a package, then right click on its name, and select 'Hibernate Package' option. On the other hand, if you want to remove a package, right click on its name and select 'Select for removal'. Once you have gone through the list, click Ok to remove unused packages.


2 Answers

You can use Code Inspection in PyCharm.

  1. Delete the contents of your requirements.txt but keep the empty file.
  2. Load your project in,
  3. PyCharm go to Code -> Inspect code....
  4. Choose Whole project option in dialog and click OK. In inspection results panel locate Package requirements section under Python (note that this section will be showed only if there is any requirements.txt or setup.py file). The section will contain one of the following messages:
  • Package requirement '<package>' is not satisfied if there is any package that is listed in requirements.txt but not used in any .py file.
  • Package '<package>' is not listed in project requirements if there is any package that is used in .py files, but not listed in requirements.txt.

You are interested in the second inspection. You can add all used packages to requirements.txt by right clicking the Package requirements section and selecting Apply Fix 'Add requirements '<package>' to requirements.txt'. Note that it will show only one package name, but it will actually add all used packages to requirements.txt if called for section.

If you want, you can add them one by one, just right click the inspection corresponding to certain package and choose Apply Fix 'Add requirements '<package>' to requirements.txt', repeat for each inspection of this kind.

After that you can create clean virtual environment and install packages from new requirements.txt.

Also note that PyCharm has import optimisation feature, see Optimize imports.... It can be useful to use this feature before any other steps listed above.

Apply fix for all packages not listed in requirements.txt

like image 194
NickAb Avatar answered Sep 29 '22 17:09

NickAb


The best bet is to use a (fresh) python venv/virtual-env with no packages, or only those you definitely know you need, test your package - installing missing packages with pip as you hit problems which should be quite quick for most software then use the pip freeze command to list the packages you really need. Better you you could use pip wheel to create a wheel with the packages in.
The other approach would be to:

  1. Use pylint to check each file for unused imports and delete them, (you should be doing this anyway),
  2. Run your tests to make sure that it was right,
  3. Use a tool like snakefood or snakefood3 to generate your new list of dependencies

Note that for any dependency checking to work well it is advisable to avoid conditional import and import within functions.

Also note that to be sure you have everything then it is a good idea to build a new venv/virtual-env and install from your dependencies list then re-test your code.

like image 31
Steve Barnes Avatar answered Sep 29 '22 16:09

Steve Barnes