How to upgrade all Python packages with pip?
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 replacegrep
+cut
withsed
orawk
orperl
or...).The
-n1
flag forxargs
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!
How to upgrade all Python packages with pip?
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 replacegrep
+cut
withsed
orawk
orperl
or...).The
-n1
flag forxargs
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!
How to update/upgrade a package using pip?
The way is
pip install <package_name> --upgrade
or in short
pip install <package_name> -U
Using sudo
will ask to enter your root password to confirm the action, but although common, is considered unsafe.
If you do not have a root password (if you are not the admin) you should probably work with virtualenv.
You can also use the user flag to install it on this user only.
pip install <package_name> --upgrade --user
Upgrade python packages from requirements.txt using pip command
No. Your requirements file has been pinned to specific versions. If your requirements are set to that version, you should not be trying to upgrade beyond those versions. If you need to upgrade, then you need to switch to unpinned versions in your requirements file.
Example:
lxml>=2.2.0
This would upgrade lxml to any version newer than 2.2.0
lxml>=2.2.0,<2.3.0
This would upgrade lxml to the most recent version between 2.2.0 and 2.3.0.
How do I update a Python package?
You might want to look into a Python package manager like pip. If you don't want to use a Python package manager, you should be able to download M2Crypto and build/compile/install over the old installation.
upgrade all outdated pip packages discarding failures
I slightly modified the command posted in the duplicate of link.
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U --user
pip3 command to upgrade all packages, that is careful about dependency conflicts?
Upgrading packages in python is never easy due to overlapping (sub)dependencies. There are some tools out there that try and help you manage. At my current job we use pip-tools. And in some projects we use poetry but I'm less happy about it's handling.
For pip-tools you define your top-level packages in requirements.in
file, which then resolves the sub(sub-sub)dependencies and outputs them into a requirements.txt
file.
The benefit of this is that you only worry about your main packages.
You can still upgrade sub dependencies if so desired.
Long story short; blindly updating all your packages will most likely never work out as intended or expected. Either packages ARE upgraded, but stop working, or they do work but don't work with another package that was updated because they needed a lower version of that package.
My advice would be to start with your main packages and build up from there using one of the tools mentioned. There isn't a silver bullet for this. Dependency hell is a very real thing in python.
Related Topics
Split a String by Spaces -- Preserving Quoted Substrings -- in Python
Using Os.Walk() to Recursively Traverse Directories in Python
How to Read a Text File into a List or an Array with Python
Creating a Simple Xml File Using Python
How to Get Week Number in Python
What Does %S Mean in a Python Format String
Dictionary Creation with Fromkeys and Mutable Objects. a Surprise
How to Create a Copy of an Object in Python
How to Convert a String of Bytes into an Int
Splitting a List into N Parts of Approximately Equal Length
Getting "Permission Denied" When Running Pip as Root on My MAC
Non-Alphanumeric List Order from Os.Listdir()
Python Re.Sub Group: Number After \Number