Uninstall all the packages installed using pip by a user on macOS High Sierra

╄→尐↘猪︶ㄣ 提交于 2019-12-11 15:17:27

问题


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'")]


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/52398022/uninstall-all-the-packages-installed-using-pip-by-a-user-on-macos-high-sierra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!