4

By mistake I installed packages in the global environment using pip. I was wondering if its a good idea to uninstall the already existing Python on the OS using the instructions provided here, and re-install it using homebrew using the instructions provided over here? Or is there any way to get rid of all packages and their dependency I installed using pip.

I'm using Python 2.7.10 on macOS High Sierra. EDIT: Problem with a suggested approach:

As suggested by CloC in the comments section, I tried uninstalling all packages from global environment by typing

pip freeze > to_delete.txt

and then

sudo -H pip uninstall -y -r to_delete.txt

However I got the following error in the terminal:

Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/pip/_internal/basecommand.py", line 141, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/pip/_internal/commands/uninstall.py", line 74, in run
    auto_confirm=options.yes, verbose=self.verbosity > 0,
  File "/Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/pip/_internal/req/req_install.py", line 864, in uninstall
    uninstalled_pathset.remove(auto_confirm, verbose)
  File "/Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/pip/_internal/req/req_uninstall.py", line 221, in remove
    renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/pip/_internal/utils/misc.py", line 276, in renames
    shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 299, in move
    copytree(src, real_dst, symlinks=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 208, in copytree
    raise Error, errors
Error: [('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/macholib/dyld.py', '/private/tmp/pip-uninstall-3QWFII/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/macholib/dyld.py', "[Errno 1] Operation not permitted: '/private/tmp/pip-uninstall-3QWFII/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/macholib/dyld.py'"), [...], "[Errno 1] Operation not permitted: '/private/tmp/pip-uninstall-3QWFII/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/macholib'")]
eigenein
  • 2,083
  • 3
  • 25
  • 43
Smit Kamal
  • 146
  • 1
  • 1
  • 9
  • You will not be able to uninstall the system python on Mac as it's part of the OS (at least not without turning SIP off). The system python also has several packages preinstalled (`numpy`, `matplotlib`, `six`, python bindings for the Cocoa framework etc), you won't be able to uninstall them either. You can only uninstall packages you've installed yourself. To avoid this issue in the future, always install packages with `--user` option to avoid polluting system dirs. – hoefling Sep 19 '18 at 07:58
  • @hoefling Thanks ! I will keep that in mind. – Smit Kamal Sep 19 '18 at 16:00

3 Answers3

15

You certainly should not resinstall python to have a fresh package installation.

You can easily uninstall all packages from you global environment by firstly listing them and uninstalling them:

pip freeze > to_delete.txt

And then:

pip uninstall -y -r to_delete.txt

If you do not want to uninstall all of them, you can delete the lines you want to keep in the to_delete.txt file created in the first step.

SivolcC
  • 3,258
  • 2
  • 14
  • 32
  • Thanks for looking into this, but I think your approach will delete all packages, including the pre-insatlled ones. I only want to delete the packages I installed using pip. – Smit Kamal Sep 19 '18 at 04:51
  • 1
    `pip freeze` outputs packages installed via `pip`. System-level packages such as `pip` itself, or `wheel` and `setuptools` are skipped. If you have any doubts, you can still run `pip freeze --user` to outputs packages installed in user-site only. – SivolcC Sep 19 '18 at 04:58
  • Thanks again, I tried but got some errors which I have added in the question. Please have a look when you are free. – Smit Kamal Sep 19 '18 at 05:15
  • Right, since you are not in a virtual env, you may need to run the commands listed using `sudo` : `sudo pip freeze > to_delete.txt` and `sudo pip uninstall -y -r to_delete.txt` – SivolcC Sep 19 '18 at 05:21
  • Yup, I did try using `sudo pip freeze > to_delete.txt` and got the output I showed in the question. – Smit Kamal Sep 19 '18 at 05:23
  • And have you tried adding the `-H` flag parameter as recommended by the error message? `sudo -H pip ...` – SivolcC Sep 19 '18 at 05:29
  • Nope, got errors again. I have updated it in the question. – Smit Kamal Sep 19 '18 at 05:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180329/discussion-between-cloc-and-smit-kamal). – SivolcC Sep 19 '18 at 07:07
1

is there any way to get rid of all packages and their dependency I installed using pip.

If you would install the packages with --user option, the solution would be simple: list all user-installed packages and uninstall them in batch:

$ pip list --format=freeze | xargs pip uninstall -y

However, since you have installed the packages system wide (using sudo), the command will get somewhat hairy. MacOS ships some packages as part of the system; these are located under /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python. You can neither uninstall those nor install additional packages in the system dir. Every package you install via sudo pip install lands under /Library/Python/2.7/site-packages to ensure the OS files are not modified or removed. The solution would thus be:

  1. List all packages locatable by pip
  2. Filter out those installed under /Library/Python/2.7/site-packages
  3. Uninstall them by name

Example in bash:

$ pip list --format=freeze | cut -d= -f1 | \
  xargs -I {} bash -c 'pip show {} | ( grep -q "Location: /Library/Python/2.7/site-packages" && echo {} )' | \
  xargs pip uninstall -y

Caution

This will uninstall ALL system packages that are not preinstalled with MacOS! This includes pip itself, if you installed it system wide, e.g. via sudo easy_install pip. Also, there might be packages that were installed by other tools, for example Virtualbox installs python bindings in a package called vboxapi - this would be also uninstalled. It might be better to just list the package names:

$ pip list --format=freeze | cut -d= -f1 | \
  xargs -I {} bash -c 'pip show {} | ( grep -q "Location: /Library/Python/2.7/site-packages" && echo {} )'

analyze the results and uninstall selected packages in a separate command.

hoefling
  • 59,418
  • 12
  • 147
  • 194
1

I know my answer is late but it may help to someone.

Windows environment:

pip freeze > unins && pip uninstall -y -r unins && del unins

Linux environemnt:

pip freeze > unins && pip uninstall -y -r unins && rm unins

Using the above commands, you can delete all the packages which you installed using pip command.

Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77