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!
In older version of pip, you can use this instead:
Works on Windows. Should be good for others too.
($ is whatever directory you’re in, in command prompt. eg. C:/Users/Username>)
do
$ pip freeze > requirements.txt
open the text file, replace the == with >= , 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.
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. Compatible with Python2.7+, Python3.4+ and pip9+, pip10+, pip18+, pip19+.
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).
回答 8
以下一线可能会有所帮助:
(点> 20.0)
pip list --format freeze --outdated | sed 's/=.*//g' | xargs -n1 pip install -U
旧版本:
pip list --format freeze --outdated | sed 's/(.*//g'| xargs -n1 pip install -U
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
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
# 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
OSX, 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 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 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.
@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.
@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).
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
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
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
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
回答 23
这似乎对我有用…
pip install -U $(pip list --outdated|awk '{printf $1" "}')