Related
How do I uninstall all packages installed by pip from my currently activated virtual environment?
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
If you have packages installed directly from github/gitlab, those will have #.
Like:
django # git+https://github.com/django.git#<sha>
You can add cut -d "#" -f1 to get just the package name that is required to uninstall it.
pip freeze | cut -d "#" -f1 | xargs pip uninstall -y
This will work for all Mac, Windows, and Linux systems.
To get the list of all pip packages in the requirements.txt file (Note: This will overwrite requirements.txt if exist else will create the new one, also if you don't want to replace old requirements.txt then give different file name in the all following command in place requirements.txt).
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)
I wanted to elevate this answer out of a comment section because it's one of the most elegant solutions in the thread. Full credit for this answer goes to #joeb.
pip uninstall -y -r <(pip freeze)
This worked great for me for the use case of clearing my user packages folder outside the context of a virtualenv which many of the above answers don't handle.
Edit: Anyone know how to make this command work in a Makefile?
Bonus: A bash alias
I add this to my bash profile for convenience:
alias pipuninstallall="pip uninstall -y -r <(pip freeze)"
Then run:
pipuninstallall
Alternative for Pipenv
If you are using pipenv, you can run:
pipenv uninstall --all
Alternative for Poetry
If you are using Poetry, run:
poetry env remove --python3.9
(Note that you need to change the version number there to match whatever your Python version is.)
This works with the latest. I think it's the shortest and most declarative way to do it.
virtualenv --clear MYENV
But why not just delete and recreate the virtualenv?
Immutability rules. Besides it's hard to remember all those piping and grepping the other solutions use.
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 snippet I regularly use
pip freeze --local | xargs pip uninstall -y
Ref: pip freeze --help
I managed it by doing the following:
Create the requirements file called reqs.txt with currently installed packages list
pip freeze > reqs.txt
Then uninstall all the packages from reqs.txt
# -y means remove the package without prompting for confirmation
pip uninstall -y -r reqs.txt
I like this method as you always have a pip requirements file to fall back on should you make a mistake. It's also repeatable, and it's cross-platform (Windows, Linux, MacOs).
Method 1 (with pip freeze)
pip freeze | xargs pip uninstall -y
Method 2 (with pip list)
pip list | awk '{print $1}' | xargs pip uninstall -y
Method 3 (with virtualenv)
virtualenv --clear MYENV
On Windows if your path is configured correctly, you can use:
pip freeze > unins && pip uninstall -y -r unins && del unins
It should be a similar case for Unix-like systems:
pip freeze > unins && pip uninstall -y -r unins && rm unins
Just a warning that this isn't completely solid as you may run into issues such as 'File not found' but it may work in some cases nonetheless
EDIT: For clarity: unins is an arbitrary file which has data written out to it when this command executes: pip freeze > unins
That file that it written in turn is then used to uninstall the aforementioned packages with implied consent/prior approval via pip uninstall -y -r unins
The file is finally deleted upon completion.
I use the --user option to uninstall all the packages installed in the user site.
pip3 freeze --user | xargs pip3 uninstall -y
For Windows users, this is what I use on Windows PowerShell
pip uninstall -y (pip freeze)
First, add all package to requirements.txt
pip freeze > requirements.txt
Then remove all
pip uninstall -y -r requirements.txt
The quickest way is to remake the virtualenv completely. I'm assuming you have a requirements.txt file that matches production, if not:
# On production:
pip freeze > reqs.txt
# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt
Using virtualenvwrapper function:
wipeenv
See wipeenv documentation
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
(adding this as an answer, because I do not have enough reputation to comment on #blueberryfields 's answer)
#blueberryfields 's answer works well, but fails if there is no package to uninstall (which can be a problem if this "uninstall all" is part of a script or makefile). This can be solved with xargs -r when using GNU's version of xargs:
pip freeze --exclude-editable | xargs -r pip uninstall -y
from man xargs:
-r, --no-run-if-empty
If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there
is no input. This option is a GNU extension.
pip3 freeze --local | xargs pip3 uninstall -y
The case might be that one has to run this command several times to get an empty pip3 freeze --local.
Best way to remove all packages from the virtual environment.
Windows PowerShell:
pip freeze > unins ; pip uninstall -y -r unins ; del unins
Windows Command Prompt:
pip freeze > unins && pip uninstall -y -r unins && del unins
Linux:
pip3 freeze > unins ; pip3 uninstall -y -r unins ; rm unins
This was the easiest way for me to uninstall all python packages.
from pip import get_installed_distributions
from os import system
for i in get_installed_distributions():
system("pip3 uninstall {} -y -q".format(i.key))
the easy robust way
cross-platform
and work in pipenv as well is:
pip freeze
pip uninstall -r requirement
by pipenv:
pipenv run pip freeze
pipenv run pip uninstall -r requirement
but won't update piplock or pipfile so be aware
Cross-platform support by using only pip:
#!/usr/bin/env python
from sys import stderr
from pip.commands.uninstall import UninstallCommand
from pip import get_installed_distributions
pip_uninstall = UninstallCommand()
options, args = pip_uninstall.parse_args([
package.project_name
for package in
get_installed_distributions()
if not package.location.endswith('dist-packages')
])
options.yes = True # Don't confirm before uninstall
# set `options.require_venv` to True for virtualenv restriction
try:
print pip_uninstall.run(options, args)
except OSError as e:
if e.errno != 13:
raise e
print >> stderr, "You lack permissions to uninstall this package.
Perhaps run with sudo? Exiting."
exit(13)
# Plenty of other exceptions can be thrown, e.g.: `InstallationError`
# handle them if you want to.
On Windows if your path is configured correctly, you can use:
pip freeze > unins && pip uninstall -y -r unins && del unins
This works on my windows system
pip freeze > packages.txt && pip uninstall -y -r packages.txt && del packages.txt
The first part pip freeze > packages.txt creates a text file with list of packages installed using pip along with the version number
The second part pip uninstall -y -r packages.txt deletes all the packages installed without asking for a confirmation prompt.
The third part del packages.txt deletes the just now created packages.txt.
This is the command that works for me:
pip list | awk '{print $1}' | xargs pip uninstall -y
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
In Command Shell of Windows, the command pip freeze | xargs pip uninstall -y won't work. So for those of you using Windows, I've figured out an alternative way to do so.
Copy all the names of the installed packages of pip from the pip freeze command to a .txt file.
Then, go the location of your .txt file and run the command pip uninstall -r *textfile.txt*
If you are using pew, you can use the wipeenv command:
pew wipeenv [env]
I simply wanted to remove packages installed by the project, and not other packages I've installed (things like neovim, mypy and pudb which I use for local dev but are not included in the app requirements). So I did:
cat requirements.txt| sed 's/=.*//g' | xargs pip uninstall -y
which worked well for me.
Select Libraries To Delete From This Folder:
C:\Users\User\AppData\Local\Programs\Python\Python310\Lib\site-packages
Why not just rm -r .venv and start over?
pip uninstall `pip freeze --user`
The --user option prevents system-installed packages from being included in the listing, thereby avoiding /usr/lib and distutils permission errors.
I recently upgraded a machine from Ubuntu Server 16.04 LTS to 18.04 LTS using command line. As a result, the built-in Python installation is upgraded from Python 3.5 to 3.6. And it seems that all the Python packages previously installed using pip3 are no longer accessible. I plan to clean up all those packages and use conda for Python package management. My question is, what is the best practice for a clean uninstallation of those inaccessible packages in this case?
The old packages installed via pip3 were primarily located under /usr/local/lib/python3.5/ and ~/.local/lib/python3.5/. But there could be other leftover files, e.g., under /usr/local/bin/. I would like to remove all of related files that came with pip3 install.
sudo pip install installs pip packages to/usr/local/lib/<python_version>/dist-packages, and apt packages to /usr/lib/<python_version>/dist-packages. Check these directories and remove the unwanted packages.
I ended up writing a bash script to call pip3 uninstall on each previously installed package iteratively.
#!/bin/bash
pypath_cmd="PYTHONPATH=$HOME/.local/lib/python3.5/site-packages"
export $pypath_cmd
echo "Uninstalling editable packages in $PYTHONPATH"
rm -f $PYTHONPATH/*.egg-link
rm -f $PYTHONPATH/easy-install.pth
pip3 freeze --all --local | cut --delimiter="=" -f 1 | while read pkg ; do
echo $pkg: $(pip3 show $pkg | grep "Location:")
pip3 uninstall -y $pkg
done
pypath_cmd="PYTHONPATH=/usr/local/lib/python3.5/dist-packages"
export $pypath_cmd
echo "Uninstalling editable packages in $PYTHONPATH"
sudo rm -f $PYTHONPATH/*.egg-link
sudo rm -f $PYTHONPATH/easy-install.pth
pip3 freeze --all --local | cut --delimiter="=" -f 1 | while read pkg ; do
echo $pkg: $(pip3 show $pkg | grep "Location:")
sudo $pypath_cmd pip3 uninstall -y --no-cache-dir $pkg
done
I am wondering what is the difference between these two commands (I have the feeling that they are identical):
sudo -H pip install <package>
pip --user install <package>
More informations:
From the sudo manpage:
-H, --set-home
Request that the security policy set the HOME environment
variable to the home directory specified by the target user's
password database entry. Depending on the policy, this may be
the default behavior.
And the pip user guide: https://pip.pypa.io/en/stable/user_guide/
Related questions:
What is the difference between pip install and sudo pip install?
What is the purpose of "pip install --user ..."? and
sudo pip install VS pip install --user
But none of them talk about the sudo -H option or the precise difference between the two.
sudo is short for 'superuser do'. It simply runs the command with root privileges, which may be useful if you are installing to a directory you normally wouldn't have access to.
However, in the examples you have given the two commands would function identically, as you don't need root privileges to pip install --user
The difference comes down to the permissions that are given to the package, and the location where the package is installed. When you run a command as root, the package will be installed with root permissions.
Here's an example:
Running sudo -H pip3 install coloredlogs results in the following:
$ sudo pip3 show coloredlogs | grep Location
Location: /usr/local/lib/python3.8/dist-packages
$ ls -l /usr/local/lib/python3.8/dist-packages
drwxr-sr-x 4 root staff 4096 Feb 25 01:14 coloredlogs
$ which coloredlogs
/usr/local/bin/coloredlogs
Running pip3 install --user <package> results in the following:
$ pip3 show coloredlogs | grep Location
Location: /home/josh/.local/lib/python3.8/site-packages
$ ls -l /home/josh/.local/lib/python3.8/site-packages
drwxrwxr-x 4 josh josh 4096 Feb 25 01:14 coloredlogs
$ which coloredlogs
coloredlogs not found
Notice the location differences between the two, and also notice that the package isn't installed on PATH when installed using the --user flag. If for some reason I wanted to directly call the package, I would need to add /home/josh/.local/bin to my PATH.
Typically, when I pip install -e from a git repo, and then do "pip freeze", it gives me git version info. For example:
$ pip install -e git://github.com/kennethreitz/requests.git#27b55a74d7b9bd2f8c60fd0ee342bcbbf40e0a66#egg=requests-dev
$ pip freeze | grep requests
-e git://github.com/kennethreitz/requests.git#27b55a74d7b9bd2f8c60fd0ee342bcbbf40e0a66#egg=requests-dev
Why is it that when I do this with the piplint package, "pip freeze" reports the version number instead of the git details?
$ pip install -e git://github.com/dcramer/piplint.git#cb2752e0c9692e4df2b2b03dec3087699e90f4da#egg=piplint
$ pip freeze | grep piplint
piplint==0.1.1
When "pip freeze" tries get vcs backend to piplint, It doesn't finds it, then it marks the package as not editable.
See:
https://github.com/pypa/pip/blob/develop/pip/init.py#L135
https://github.com/pypa/pip/blob/develop/pip/vcs/init.py#L60
Maybe it's a bug in pip or in piplint setup.py.
Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
Is it possible to upgrade all Python packages at one time with pip?
Note: that there is a feature request for this on the official issue tracker.
There isn't a built-in flag yet. Starting with pip version 22.3, the --outdated and --format=freeze have become mutually exclusive. Use Python, to parse the JSON output:
pip --disable-pip-version-check list --outdated --format=json | python -c "import json, sys; print('\n'.join([x['name'] for x in json.load(sys.stdin)]))"
If you are using pip<22.3 you can use:
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
For older versions of pip:
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
The grep is to skip editable ("-e") package definitions, as suggested by #jawache. (Yes, you could replace grep+cut with sed or awk or perl or...).
The -n1 flag for xargs prevents stopping everything if updating one package fails (thanks #andsens).
Note: there are infinite potential variations for this. I'm trying to keep this answer short and simple, but please do suggest variations in the comments!
You can use the following Python code. Unlike pip freeze, this will not print warnings and FIXME errors.
For pip < 10.0.1
import pip
from subprocess import call
packages = [dist.project_name for dist in pip.get_installed_distributions()]
call("pip install --upgrade " + ' '.join(packages), shell=True)
For pip >= 10.0.1
import pkg_resources
from subprocess import call
packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)
To upgrade all local packages, you can install pip-review:
$ pip install pip-review
After that, you can either upgrade the packages interactively:
$ pip-review --local --interactive
Or automatically:
$ pip-review --local --auto
pip-review is a fork of pip-tools. See pip-tools issue mentioned by #knedlsepp. pip-review package works but pip-tools package no longer works. pip-review is looking for new maintainer.
pip-review works on Windows since version 0.5.
The following works on Windows and should be good for others too ($ is whatever directory you're in, in the command prompt. For example, C:/Users/Username).
Do
$ pip freeze > requirements.txt
Open the text file, replace the == with >=, or have sed do it for you:
$ sed -i 's/==/>=/g' requirements.txt
and execute:
$ pip install -r requirements.txt --upgrade
If you have a problem with a certain package stalling the upgrade (NumPy sometimes), just go to the directory ($), comment out the name (add a # before it) and run the upgrade again. You can later uncomment that section back. This is also great for copying Python global environments.
Another way:
I also like the pip-review method:
py2
$ pip install pip-review
$ pip-review --local --interactive
py3
$ pip3 install pip-review
$ py -3 -m pip-review --local --interactive
You can select 'a' to upgrade all packages; if one upgrade fails, run it again and it continues at the next one.
Use pipupgrade! ... last release 2019
pip install pipupgrade
pipupgrade --verbose --latest --yes
pipupgrade helps you upgrade your system, local or packages from a requirements.txt file! It also selectively upgrades packages that don't break change.
pipupgrade also ensures to upgrade packages present within multiple Python environments. It is compatible with Python 2.7+, Python 3.4+ and pip 9+, pip 10+, pip 18+, pip 19+.
Note: I'm the author of the tool.
Windows version after consulting the excellent documentation for FOR by Rob van der Woude:
for /F "delims===" %i in ('pip freeze') do pip install --upgrade %i
This option seems to me more straightforward and readable:
pip install -U `pip list --outdated | awk 'NR>2 {print $1}'`
The explanation is that pip list --outdated outputs a list of all the outdated packages in this format:
Package Version Latest Type
--------- ------- ------ -----
fonttools 3.31.0 3.32.0 wheel
urllib3 1.24 1.24.1 wheel
requests 2.20.0 2.20.1 wheel
In the AWK command, NR>2 skips the first two records (lines) and {print $1} selects the first word of each line (as suggested by SergioAraujo, I removed tail -n +3 since awk can indeed handle skipping records).
The following one-liner might prove of help:
(pip > 20.0)
pip list --format freeze --outdated | sed 's/=.*//g' | xargs -n1 pip install -U
Older Versions:
pip list --format freeze --outdated | sed 's/(.*//g' | xargs -n1 pip install -U
xargs -n1 keeps going if an error occurs.
If you need more "fine grained" control over what is omitted and what raises an error you should not add the -n1 flag and explicitly define the errors to ignore, by "piping" the following line for each separate error:
| sed 's/^<First characters of the error>.*//'
Here is a working example:
pip list --format freeze --outdated | sed 's/=.*//g' | sed 's/^<First characters of the first error>.*//' | sed 's/^<First characters of the second error>.*//' | xargs pip install -U
You can just print the packages that are outdated:
pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'
More Robust Solution
For pip3, use this:
pip3 freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip3 install -U \1/p' |sh
For pip, just remove the 3s as such:
pip freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip install -U \1/p' |sh
OS X Oddity
OS X, as of July 2017, ships with a very old version of sed (a dozen years old). To get extended regular expressions, use -E instead of -r in the solution above.
Solving Issues with Popular Solutions
This solution is well designed and tested1, whereas there are problems with even the most popular solutions.
Portability issues due to changing pip command line features
Crashing of xargs because of common pip or pip3 child process failures
Crowded logging from the raw xargs output
Relying on a Python-to-OS bridge while potentially upgrading it3
The above command uses the simplest and most portable pip syntax in combination with sed and sh to overcome these issues completely. Details of the sed operation can be scrutinized with the commented version2.
Details
[1] Tested and regularly used in a Linux 4.8.16-200.fc24.x86_64 cluster and tested on five other Linux/Unix flavors. It also runs on Cygwin64 installed on Windows 10. Testing on iOS is needed.
[2] To see the anatomy of the command more clearly, this is the exact equivalent of the above pip3 command with comments:
# Match lines from pip's local package list output
# that meet the following three criteria and pass the
# package name to the replacement string in group 1.
# (a) Do not start with invalid characters
# (b) Follow the rule of no white space in the package names
# (c) Immediately follow the package name with an equal sign
sed="s/^([^=# \t\\][^ \t=]*)=.*"
# Separate the output of package upgrades with a blank line
sed="$sed/echo"
# Indicate what package is being processed
sed="$sed; echo Processing \1 ..."
# Perform the upgrade using just the valid package name
sed="$sed; pip3 install -U \1"
# Output the commands
sed="$sed/p"
# Stream edit the list as above
# and pass the commands to a shell
pip3 freeze --local | sed -rn "$sed" | sh
[3] Upgrading a Python or PIP component that is also used in the upgrading of a Python or PIP component can be a potential cause of a deadlock or package database corruption.
I had the same problem with upgrading. Thing is, I never upgrade all packages. I upgrade only what I need, because project may break.
Because there was no easy way for upgrading package by package, and updating the requirements.txt file, I wrote this pip-upgrader which also updates the versions in your requirements.txt file for the packages chosen (or all packages).
Installation
pip install pip-upgrader
Usage
Activate your virtualenv (important, because it will also install the new versions of upgraded packages in current virtualenv).
cd into your project directory, then run:
pip-upgrade
Advanced usage
If the requirements are placed in a non-standard location, send them as arguments:
pip-upgrade path/to/requirements.txt
If you already know what package you want to upgrade, simply send them as arguments:
pip-upgrade -p django -p celery -p dateutil
If you need to upgrade to pre-release / post-release version, add --prerelease argument to your command.
Full disclosure: I wrote this package.
This seems more concise.
pip list --outdated | cut -d ' ' -f1 | xargs -n1 pip install -U
Explanation:
pip list --outdated gets lines like these
urllib3 (1.7.1) - Latest: 1.15.1 [wheel]
wheel (0.24.0) - Latest: 0.29.0 [wheel]
In cut -d ' ' -f1, -d ' ' sets "space" as the delimiter, -f1 means to get the first column.
So the above lines becomes:
urllib3
wheel
Then pass them to xargs to run the command, pip install -U, with each line as appending arguments.
-n1 limits the number of arguments passed to each command pip install -U to be 1.
One-liner version of Ramana's answer.
python -c 'import pip, subprocess; [subprocess.call("pip install -U " + d.project_name, shell=1) for d in pip.get_installed_distributions()]'
From yolk:
pip install -U `yolk -U | awk '{print $1}' | uniq`
However, you need to get yolk first:
sudo pip install -U yolk
The pip_upgrade_outdated (based on this older script) does the job. According to its documentation:
usage: pip_upgrade_outdated [-h] [-3 | -2 | --pip_cmd PIP_CMD]
[--serial | --parallel] [--dry_run] [--verbose]
[--version]
Upgrade outdated python packages with pip.
optional arguments:
-h, --help show this help message and exit
-3 use pip3
-2 use pip2
--pip_cmd PIP_CMD use PIP_CMD (default pip)
--serial, -s upgrade in serial (default)
--parallel, -p upgrade in parallel
--dry_run, -n get list, but don't upgrade
--verbose, -v may be specified multiple times
--version show program's version number and exit
Step 1:
pip install pip-upgrade-outdated
Step 2:
pip_upgrade_outdated
When using a virtualenv and if you just want to upgrade packages added to your virtualenv, you may want to do:
pip install `pip freeze -l | cut --fields=1 -d = -` --upgrade
The simplest and fastest solution that I found in the pip issue discussion is:
pip install pipdate
pipdate
Source: https://github.com/pypa/pip/issues/3819
Windows PowerShell solution
pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}
Use AWK update packages:
pip install -U $(pip freeze | awk -F'[=]' '{print $1}')
Windows PowerShell update
foreach($p in $(pip freeze)){ pip install -U $p.Split("=")[0]}
Updating Python packages on Windows or Linux
Output a list of installed packages into a requirements file (requirements.txt):
pip freeze > requirements.txt
Edit requirements.txt, and replace all ‘==’ with ‘>=’. Use the ‘Replace All’ command in the editor.
Upgrade all outdated packages
pip install -r requirements.txt --upgrade
Source: How to Update All Python Packages
One line in PowerShell 5.1 with administrator rights, Python 3.6.5, and pip version 10.0.1:
pip list -o --format json | ConvertFrom-Json | foreach {pip install $_.name -U --no-warn-script-location}
It works smoothly if there are no broken packages or special wheels in the list...
If you have pip<22.3 installed, a pure Bash/Z shell one-liner for achieving that:
for p in $(pip list -o --format freeze); do pip install -U ${p%%=*}; done
Or, in a nicely-formatted way:
for p in $(pip list -o --format freeze)
do
pip install -U ${p%%=*}
done
After this you will have pip>=22.3 in which -o and --format freeze are mutually exclusive, and you can no longer use this one-liner.
The rather amazing yolk makes this easy.
pip install yolk3k # Don't install `yolk`, see https://github.com/cakebread/yolk/issues/35
yolk --upgrade
For more information on yolk: https://pypi.python.org/pypi/yolk/0.4.3
It can do lots of things you'll probably find useful.
You can try this:
for i in ` pip list | awk -F ' ' '{print $1}'`; do pip install --upgrade $i; done
The shortest and easiest on Windows.
pip freeze > requirements.txt && pip install --upgrade -r requirements.txt && rm requirements.txt
Use:
pip install -r <(pip freeze) --upgrade
Ramana's answer worked the best for me, of those here, but I had to add a few catches:
import pip
for dist in pip.get_installed_distributions():
if 'site-packages' in dist.location:
try:
pip.call_subprocess(['pip', 'install', '-U', dist.key])
except Exception, exc:
print exc
The site-packages check excludes my development packages, because they are not located in the system site-packages directory. The try-except simply skips packages that have been removed from PyPI.
To endolith: I was hoping for an easy pip.install(dist.key, upgrade=True), too, but it doesn't look like pip was meant to be used by anything but the command line (the docs don't mention the internal API, and the pip developers didn't use docstrings).
There is not necessary to be so troublesome or install some package.
Update pip packages on Linux shell:
pip list --outdated --format=freeze | awk -F"==" '{print $1}' | xargs -i pip install -U {}
Update pip packages on Windows powershell:
pip list --outdated --format=freeze | ForEach { pip install -U $_.split("==")[0] }
Some points:
Replace pip as your python version to pip3 or pip2.
pip list --outdated to check outdated pip packages.
--format on my pip version 22.0.3 only has 3 types: columns (default), freeze, or json. freeze is better option in command pipes.
Keep command simple and usable as many systems as possible.
Sent through a pull-request to the pip folks; in the meantime use this pip library solution I wrote:
from pip import get_installed_distributions
from pip.commands import install
install_cmd = install.InstallCommand()
options, args = install_cmd.parse_args([package.project_name
for package in
get_installed_distributions()])
options.upgrade = True
install_cmd.run(options, args) # Chuck this in a try/except and print as wanted
This ought to be more effective:
pip3 list -o | grep -v -i warning | cut -f1 -d' ' | tr " " "\n" | awk '{if(NR>=3)print}' | cut -d' ' -f1 | xargs -n1 pip3 install -U
pip list -o lists outdated packages;
grep -v -i warning inverted match on warning to avoid errors when updating
cut -f1 -d1' ' returns the first word - the name of the outdated package;
tr "\n|\r" " " converts the multiline result from cut into a single-line, space-separated list;
awk '{if(NR>=3)print}' skips header lines
cut -d' ' -f1 fetches the first column
xargs -n1 pip install -U takes 1 argument from the pipe left of it, and passes it to the command to upgrade the list of packages.