18

I want to revert my Python install back to its base state so I can start using virtualenv. Is there an easy way to uninstall only those packages that have been installed after Python was set up?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Cam
  • 2,026
  • 3
  • 25
  • 42
  • 2
    Why not just uninstall everything including python? Also maybe add in what OS you are using. – Dan Jul 03 '19 at 13:02
  • 4
    If you're using Ubuntu, don't uninstall Python. There's a lot of things that depend on it. – D Malan Jul 03 '19 at 13:05
  • 1
    @Dan uninstalling Python won't remove pip or the packages it installed. – Cam Jul 03 '19 at 13:33
  • this solution worked for me https://stackoverflow.com/questions/11248073/what-is-the-easiest-way-to-remove-all-packages-installed-by-pip/67379806#67379806 – Sathiamoorthy Aug 03 '21 at 12:28

2 Answers2

72

The following command should do the trick:

pip freeze > requirements.txt && pip uninstall -r requirements.txt -y

Alternatively you can skip the creation of any intermediate files (i.e. requirements.txt):

pip uninstall -y -r <(pip freeze)
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
  • 1
    just to add to this as an extension, `pip3 uninstall -y -r <(pip3 freeze)` – Vinny Feb 23 '21 at 03:35
  • This works too well! Don't run it in your base conda environment, for example, because it will remove some things required for conda itself to work. – someone Dec 01 '22 at 16:30
12

do following

  1. store all the pip packages in requirements.txt

    python -m pip freeze > requirements.txt
    
  2. remove all pip packages which menetioned in requirements.txt

    python -m pip uninstall -r requirements.txt
    
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • When I did this, it thrown me a fatal python error `Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding`, which states that PYTHONHOME and PYTHONPATH is not set – Ipvikukiepki-KQS Dec 12 '20 at 18:30
  • @Naras-KS this may [help you](https://stackoverflow.com/questions/30767191/fatal-python-error-py-initialize-unable-to-load-the-file-system-codec-importe) – sahasrara62 Dec 12 '20 at 19:33
  • 1
    Thanks for the information. The error persists even after specifying the respective path to the environment variables, which I did manually. So, I have uninstalled it and reinstalled the python @sahasrara62 – Ipvikukiepki-KQS Dec 12 '20 at 19:46
  • i sugges you to create a new question stating your problem, so other can help you better – sahasrara62 Dec 12 '20 at 19:55