What is the easiest way to remove all packages ins

2019-01-05 06:46发布

I'm trying to fix up one of my virtualenvs - I'd like to reset all of the installed libraries back to the ones that match production.

Is there a quick and easy way to do this with pip?

18条回答
ゆ 、 Hurt°
2楼-- · 2019-01-05 07:14

Other answers that use pip list or pip freeze must include --local else it will also uninstall packages that are found in the common namespaces.

So here are the snippets I regularly use

 pip freeze --local | xargs pip uninstall -y

or

 pip list --local | py -x "print(x.split()[0])" | xargs pip uninstall -y

Learn more about this behavior by issuing pip freeze --help

查看更多
三岁会撩人
3楼-- · 2019-01-05 07:16

This will work for all Mac, Windows and Linux System. To get the list of all pip package in the requirements.txt file (Note: This will overwrite requirements.txt if exist else will create the new one.)

pip freeze > requirements.txt

Now to remove one by one

pip uninstall -r requirements.txt

If we want to remove all at once then

pip uninstall -r requirements.txt -y

If you're working on an existing project that has a requirements.txt file and your environment has diverged, simply replace requirements.txt from the above examples with toberemoved.txt. Then, once you have gone through the steps above, you can use the requirements.txt to update your now clean environment.

And For single command without creating any file (As joeb suggested).

pip uninstall -y -r <(pip freeze)
查看更多
beautiful°
4楼-- · 2019-01-05 07:18

Its an old question I know but I did stumble across it so for future reference you can now do this:

pip uninstall [options] <package> ...
pip uninstall [options] -r <requirements file> ...

-r, --requirement file

Uninstall all the packages listed in the given requirements file. This option can be used multiple times.

from the pip documentation version 8.1

查看更多
ら.Afraid
5楼-- · 2019-01-05 07:20

I've found this snippet as an alternative solution. It's a more graceful removal of libraries than remaking the virtualenv:

pip freeze | xargs pip uninstall -y

In case you have packages installed via VCS, you need to exclude those lines and remove the packages manually (elevated from the comments below):

pip freeze | grep -v "^-e" | xargs pip uninstall -y
查看更多
倾城 Initia
6楼-- · 2019-01-05 07:22

If you're running virtualenv:

virtualenv --clear </path/to/your/virtualenv>

for example, if your virtualenv is /Users/you/.virtualenvs/projectx, then you'd run:

virtualenv --clear /Users/you/.virtualenvs/projectx

if you don't know where your virtual env is located, you can run which python from within an activated virtual env to get the path

查看更多
叼着烟拽天下
7楼-- · 2019-01-05 07:22

Pip has no way of knowing what packages were installed by it and what packages were installed by your system's package manager. For this you would need to do something like this

for rpm-based distros (replace python2.7 with your python version you installed pip with):

find /usr/lib/python2.7/ |while read f; do
  if ! rpm -qf "$f" &> /dev/null; then
    echo "$f"
  fi
done |xargs rm -fr

for a deb-based distribution:

find /usr/lib/python2.7/ |while read f; do
  if ! dpkg-query -S "$f" &> /dev/null; then
    echo "$f"
  fi
done |xargs rm -fr

then to clean up empty directories left over:

find /usr/lib/python2.7 -type d -empty |xargs rm -fr

I found the top answer very misleading since it will remove all (most?) python packages from your distribution and probably leave you with a broken system.

查看更多
登录 后发表回答