How do I update a Python package?
Asked Answered
W

13

344

I'm running Ubuntu 9:10 and a package called M2Crypto is installed (version is 0.19.1). I need to download, build and install the latest version of the M2Crypto package (0.20.2).

The 0.19.1 package has files in a number of locations including (/usr/share/pyshared and /usr/lib/pymodules.python2.6).

How can I completely uninstall version 0.19.1 from my system before installing 0.20.2?

Wadi answered 3/3, 2011 at 16:40 Comment(0)
H
68

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.

Heffron answered 3/3, 2011 at 16:43 Comment(1)
Thanks. What you said worked. I built and installed 0.20.2 without needing to uninstall 0.19.1.Wadi
A
624

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
Arriola answered 28/11, 2014 at 7:34 Comment(6)
And if you are using a proxy without authentication: sudo pip install [package] --upgrade --proxy=address:portCousteau
I came here for this answer. I tried pip update and pip upgrade and was desperate until I saw --upgrade here. Why don't they add a pip upgrade command?Jacintojack
This is indeed a well-hidden feature.Leopold
For Windows users: sudo is not required.Ohare
This should be answer #1Ruthieruthless
For people who is using a server without root privilege, try pip3 install [package_name] --upgrade --userButchery
H
68

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.

Heffron answered 3/3, 2011 at 16:43 Comment(1)
Thanks. What you said worked. I built and installed 0.20.2 without needing to uninstall 0.19.1.Wadi
L
57

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!

Letrice answered 28/8, 2017 at 11:6 Comment(8)
i got a syntax error pointing to the last bracket in: awk: cmd. line:1: { print $1 })Paddlefish
@TT Newer versions of pip require you to use the --format=legacy option, i.e., pip list --outdated --format=legacy. Also FYI everyone: blindly updating all modules via pip can be quite dangerous on many Linux distros. Many of them provide specific python modules via distro packages and some of those distros (RHEL in particular) can break hard if you updating shit ... not to mention the fact that if you update via pip, the distro packages might revert your changes on a future update.Waybill
thanks @rsaw. He's right you should run your pip commands with the --user optionLetrice
legacy was an invalid format for me in PIP 19.0. Recommended alternative: pip install --upgrade --user $(pip list --outdated --user --format=freeze | cut -d'=' -f1) (Yes, I am terrible with awk, hence the usage of cut.) Leave out --user where appropriate depending on your needs.Welldisposed
I like this answer, though I understand the danger in updating everything automatically. Anyway, since legacy is deprecated in pip 19.0 pip install $(pip list --outdated --format=columns | grep -v "Package" | grep -v -- ---- | awk '{ print $1 }') --upgradeSalters
In order to work on an Ubuntu 18.04 venv adapt as follows: pip install $(pip list --outdated | tail +3 | grep -v sdist | awk '{ print $1 }') --upgradeElizaelizabet
why is this not a built in feature of pip?Spode
@SamJohnson sorry for replying 2.5 years late but this is primarily due to compatibility. If you upgrade and a package has breaking changes, then upgrading would result your program to become dysfunctional.Letrice
W
44
  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:

pip install --upgrade pip

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

Wilder answered 7/6, 2017 at 12:22 Comment(1)
You can omit "py -m" part.Ferryman
C
40
  • 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
Cremator answered 10/2, 2018 at 0:52 Comment(0)
R
14

I think the best one-liner is:

pip install --upgrade <package>==<version>
Rann answered 5/12, 2017 at 20:12 Comment(0)
O
10

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
Overweary answered 21/8, 2020 at 9:46 Comment(0)
B
4

How was the package originally installed? If it was via apt, you could just be able to do apt-get remove python-m2crypto

If you installed it via easy_install, I'm pretty sure the only way is to just trash the files under lib, shared, etc..

My recommendation in the future? Use something like pip to install your packages. Furthermore, you could look up into something called virtualenv so your packages are stored on a per-environment basis, rather than solely on root.

With pip, it's pretty easy:

pip install m2crypto

But you can also install from git, svn, etc repos with the right address. This is all explained in the pip documentation

Brause answered 3/3, 2011 at 16:44 Comment(1)
The way he describes where the files are installed indicates that the package was installed using the apt package manager - because (on Debian AFAIK) apt would install python packages under the 2 locations he listed - /share/pyshared/.. is for shared code across versions and /usr/lib/.. for version specific parts. easy-install/pip on the other hand would use /usr/local/lib/.. - so as you said - just apt-get remove would've done him.Aquamanile
C
4
pip install -U $(pip list --outdated | awk 'NR>2 {print $1}')
Cerated answered 31/3, 2018 at 5:32 Comment(1)
You should improve your answer by adding an explanation. Especially it needs clarification, how this differs from already given answers.Kavanaugh
G
3

In Juptyer notebook, a very simple way is

!pip install <package_name> --upgrade

So, you just need to replace with the actual package name.

Giglio answered 29/3, 2019 at 20:47 Comment(0)
S
3

How can I completely uninstall version 0.19.1 from my system before installing 0.20.2?

In order to uninstall M2Crypto use

pip uninstall M2Crypto

I need to download, build and install the latest version of the M2Crypto package (0.20.2).

In order to install the latest version, one can use PyPi

pip install M2Crypto 

To install the version 20.2 (an outdated one), run

pip install M2Crypto==0.20.2

Assuming one just wants to upgrade

pip install M2Crypto --upgrade # Or pip install M2Crypto -U

Notes:

  • Depending on one's Python version (here's how to find the version) one may use a different pip command. Let's say one is working with Python 3.7, instead of just using pip, one might use pip3.7.

  • Using sudo is considered unsafe.

  • Nowadays there are better practices to manage the development system, such as: virtual environments or development containers. The development containers allow one to put the entire development environment (be it modules, VS Code extensions, npm libraries,...) inside a Docker container. When the project comes to an end, one closes the container. There's no need to keep all of those requirements around in the computer for no reason. If you feel like reading more about it: Visual Studio Docs, Github.

Sallyanne answered 22/5, 2021 at 9:15 Comment(0)
L
2

Get all the outdated packages and create a batch file with the following commands pip install xxx --upgrade for each outdated packages

Liebermann answered 21/10, 2017 at 15:17 Comment(0)
C
0

I.e.:

python -m pip install --proxy <proxyserver_name>:<port#> <pkg_name> 

Remember to export the variables after setting them, to make them available to the outer shell session.

Windows:

Add to environment variables:

set HTTP_PROXY=<proxyserver_name>:<port#>

You might have to install the full python package first

Coriss answered 12/8, 2022 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.