1

I was playing with Python and Machine Learning. During the experimental phase, I added more and more stuff to the requirements.txt file.

Now that I know what code I want to keep, I have deleted those experiments which are not helpful. Thus, some requirements have become obsolete. I'm now looking for a way to clean up my requirements.txt file.

Initially I thought I could just clear the file entirely and then go through the import statements and let PyCharm add them. However, that's not a good idea, because it will just add the latest version of the library, which isn't always possible. I need some libraries in special versions.

Is there a good way to clean up the requirements? I'm thinking of an action similar to "Optimize imports" (PyCharm) or "remove unused variable" or "Remove unused libraries" (Resharper).

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • 1
    For the record: here is a reference on _how to remove the requirements and add again all that are used in the project_. You state explicitly that this is not enough (due to specific versions) but it may be useful to other readers, for context. https://stackoverflow.com/questions/25376213/delete-unused-packages-from-requirements-file – stan0 Feb 05 '20 at 14:31

1 Answers1

1

I think I found a pypi package that could be useful - pip_check_reqs.

It looks like there is a tool in it - pip-extra-reqs that

would find anything that is listed in requirements.txt but that is not imported by sample.

I guess in this example "sample" is your python module:

pip-extra-reqs --ignore-file=sample/tests/* sample

I would give that package a try.

stan0
  • 11,549
  • 6
  • 42
  • 59