90

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
myildirim
  • 2,248
  • 2
  • 19
  • 25
  • I recently released [deptry](https://fpgmaas.github.io/deptry/), a command line utility that checks for various issues with a project's dependencies, such as obsolete, missing or transitive dependencies. It supports `requirement.txt` files since version 0.4.0. – Florian Sep 12 '22 at 06:53

5 Answers5

135

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

Ali Husham
  • 816
  • 10
  • 31
NickAb
  • 6,337
  • 3
  • 15
  • 18
  • 1
    Code inspection was useful to me. Thanks. – Eli May 04 '16 at 07:40
  • I followed these instructions with version 2016.2, build 162.1237.1 and it missed several of the imports. Seems like it has troubles with importing names from modules. For example, at the top of one of my files is: `from rest_framework import status` and it didn't add `rest_framework` to my requirements file using the above steps – Nwilson Jul 27 '16 at 23:11
  • 3
    Whilst this is a good attempt, it's not a great solution for packages whose module names differ from the package names, as @Nwilson pointed out. Also, PyCharm will not add Version numbers to the requirements, this is problematic for anyone trying to clean up a project in production. – Daniel van Flymen May 17 '17 at 20:35
  • Have any way to auto add specify version in PyCharm ? – Vic Mar 04 '20 at 08:41
  • 7
    Update for newer versions of PyCharm (works in 2020.3 Professional for me): Instead of starting from scratch and building up a new requirements.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). – brianmaissy Dec 08 '20 at 08:44
  • 1
    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. initialized) and then follow the answer compse by @NickAb. – Franco Gil May 07 '21 at 17:05
23

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.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • Thanks for answer.We also use virtual-env (the app is on the production also)This question raises during a refactoring.That's why I asked. – myildirim Aug 19 '14 at 05:40
  • 1
    Note that `snakefood` does not support `python3`, and the URL to the source on the website is wrong currently, it's: https://github.com/blais/snakefood – miyalys Jul 15 '21 at 10:52
  • 2
    @miyalys - thanks for flagging this up - a quick search on pypi throws up snakefood3 (https://github.com/Trim21/snakefood3) which I have yet to try as I prefer to use a bare venv as a starting point. – Steve Barnes Jul 15 '21 at 18:01
9

You can find obsolete dependencies by using deptry, a command line utility that checks for various issues with a project's dependencies, such as unused, missing or transitive dependencies.

Add it to your project with

pip install deptry

and then run

deptry .

Example output:

Scanning 2 files...

requirements.txt: DEP002 'pandas' defined as a dependency but not used in the codebase
Found 1 dependency issue.

Note that for the best results, you should be using a virtual environment for your project, see e.g. here.

Disclaimer: I am the author of deptry.

Florian
  • 24,425
  • 4
  • 49
  • 80
  • 1
    thanks for this tool @Florian, I tried it locally on one of my pet projects, works fine, but gave the following dependency error for `util` and `views` imports, which were false alarms: --- There are dependencies missing from the project's list of dependencies: util views Consider adding them to your project's dependencies. – pranavcode Sep 21 '22 at 17:44
  • Thanks @pranavcode for the feedback. For now, you can ignore those by running deptry as `deptry . -io util,views`, or adding configuration to `pyproject.toml`. Could you elaborate why they are false alarms? `util` and `views` do not seem to be part of the standard library. `deptry` should also ignore relative imports and local directories with an `__init__.py` file. Would you consider raising an issue in the GitHub repository? I'd be happy to improve `deptry` based on your feedback. – Florian Sep 22 '22 at 08:25
  • @pranavcode I was finally able to reproduce the bug you described. It's solved in release `0.6.2` by [this](https://github.com/fpgmaas/deptry/pull/163) Merge Request. – Florian Oct 24 '22 at 08:00
  • @Florian the tool would be great, but currently it has some packages marked as "The project contains obsolete dependencies", which are not. This unfortunately makes the package unusable for a real project – HAL9000 Mar 16 '23 at 13:58
  • @HAL9000 Sorry for the late reply and thanks for your feedback. I am curious for the cause of your issue, I have not encountered that problem myself yet. As a workaround, you can configure deptry to ignore them as follows: `deptry . --ignore-obsolete "foo,bar"` – Florian May 08 '23 at 16:19
8

In pycharm go to Tools -> Sync Python Requirements. There's a 'Remove unused requirements' checkbox.

Diego Portillo
  • 103
  • 1
  • 7
  • it works like a ...charm! This is the smoothest, cleanest, fastest solution among all answers. – steel00 Jun 09 '23 at 14:19
  • uhmmm does this just use the interpreter you configured to sync that with the requirements.txt file? What if the interpreter has extraneous packages? – user3180 Aug 09 '23 at 23:21
  • What if the interpreter has extraneous packages? Go to File -> Settings -> Project -> Python Interpreter to remove the packages that you don't use. – Diego Portillo Aug 10 '23 at 17:24
  • does this just use the interpreter you configured to sync that with the requirements.txt file? yes – Diego Portillo Aug 10 '23 at 17:24
0

I've used with success pip-check-reqs.

With command pip-extra-reqs your_directory it will check for all unused dependencies in your_directory

Install it with pip install pip-check-reqs.

siulkilulki
  • 1,054
  • 1
  • 10
  • 18
  • This tool is unable to read requirements from `setup.cfg` file, which is still the most portable place to specify them in 2020 (due to lack of consensus around pyproject.toml format). – sorin Oct 29 '20 at 08:55
  • 2
    Update: Version 2.2 added `--requirements-file` and `--skip-incompatible` flag (full disclosure: I did the PRs). We use it extensively at work for around ~200 libraries and have no trouble keeping the requirements clean that way. – Błażej Michalik Mar 09 '21 at 02:30
  • @sorin I might try implementing it, if you point me at a good example. Also, didn't the usage of `setup.cfg` alone [already mandate the usage of `pyproject.toml`?](https://setuptools.readthedocs.io/en/latest/setuptools.html#setup-cfg-only-projects). – Błażej Michalik Mar 09 '21 at 02:43
  • Using `pip-missing-reqs my_app_folder_container` could work if your project doesn't contain a complex structure | architecture. I use this package inside of a FastApi project with a "moderate size" and works pretty nice. – Franco Gil May 07 '21 at 17:01
  • 2
    Update from 2022, I used pip-check-reqs. it works – kay fresh Apr 01 '22 at 14:13