Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update a Python package?

People also ask

How do I upgrade pip package?

Update a package: pip install --upgrade To update installed packages to the latest version, run pip install with the --upgrade or -U option.

Can I update Python with pip?

pip is Python's official package manager and is a recommended method for installing, upgrading, and uninstalling the Python packages.


The best way I've found is to run this command from terminal

sudo pip install [package_name] --upgrade

sudo will ask to enter your root password to confirm the action.


Note: Some users may have pip3 installed instead. In that case, use

sudo pip3 install [package_name] --upgrade

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.


To automatically upgrade all the outdated packages (that were installed using pip), just run the script bellow,

pip install $(pip list --outdated | awk '{ print $1 }') --upgrade

Here, pip list --outdated will list all the out dated packages and then we pipe it to awk, so it will print only the names. Then, the $(...) will make it a variable and then, everything is done auto matically. Make sure you have the permissions. (Just put sudo before pip if you're confused) I would write a script named, pip-upgrade The code is bellow,

#!/bin/bash
sudo pip install $(pip list --outdated | awk '{ print $1 }') --upgrade

Then use the following lines of script to prepare it:

sudo chmod +x pip-upgrade
sudo cp pip-upgrade /usr/bin/

Then, just hit pip-upgrade and voila!


  1. Via windows command prompt, run: pip list --outdated You will get the list of outdated packages.
  2. Run: pip install [package] --upgrade It will upgrade the [package] and uninstall the previous version.

To update pip:

py -m pip install --upgrade pip

Again, this will uninstall the previous version of pip and will install the latest version of pip.


  • Method 1: Upgrade manually one by one

pip install package_name -U
  • Method 2: Upgrade all at once (high chance rollback if some package fail to upgrade

pip install $(pip list --outdated --format=columns |tail -n +3|cut -d" " -f1) --upgrade
  • Method 3: Upgrade one by one using loop

for i in  $(pip list --outdated --format=columns |tail -n +3|cut -d" " -f1); do pip install $i --upgrade; done

I think the best one-liner is:

pip install --upgrade <package>==<version>

Open Command prompt or terminal and use below syntax

pip install --upgrade [package]==[specific version or latest version]

For Example

pip install --upgrade numpy==1.19.1