Explain Codes LogoExplain Codes Logo

How do I update a Python package?

python
pip-install
package-management
virtual-environments
Alex KataevbyAlex Kataev·Nov 15, 2024
TLDR

For an express upgrade of a Python package, use:

pip install -U package_name

The -U flag prompts pip to grab the latest version, swiftly replacing your outdated one.

Using 'sudo' with pip

If you aspire to be the boss of your system, treat sudo as your magic spell. Command system-wide installation and never look back:

# Use sudo only if you've grown a full wizard beard! sudo pip install -U package_name

For user-specific installations, either a virtual environment or the --user flag is more appropriate:

# User bob gets a private installation. Keeping the good stuff to yourself, bob? pip install --user -U package_name

Locating and upgrading old packages

Playing detective with outdated packages? Here's your magnifying glass:

# "Old is Gold", but not this time. pip list --outdated

Cure all your outdated packages in one massive swoop for a power move:

# Because ain't nobody got time for one-by-one upgrades pip install $(pip list --outdated | awk '{print $1}') -U

Facing special case results

M2Crypto pushing you to the edge? Toss the old version right into the trash, and replace it with shiny and new:

# Time to say goodbye to the old M2Crypto sudo pip uninstall M2Crypto # Welcome home, M2Crypto v.0.20.2! sudo pip install M2Crypto==0.20.2

Life didn't turn out as expected post-update? Time to love the ex (previous version):

# Embrace your past with Python! pip install package_name==previous_version

Automation with the power of scripts

Why manually upgrade packages when you can make it automatic? Scripts like pip-upgrade are your saving grace.

Cleaning up package residue

Leftover files after uninstallation? Eww. Let's clean up to maintain hygiene:

Upgrading pip, the head honcho

Keeping up with the times means your lead roadrunner, pip, also needs to be at its fittest:

# Can't let the boss be outdated, can we? pip install --upgrade pip

Upgrading into unknowns

Diving straight into an upgrade? The blindfold option:

# Going with the flow pip install --upgrade package_name

A little bit adventurous? Update the package, and take the companions (dependencies) along:

# A complete package deal, 'eager' for more. pip install --upgrade --upgrade-strategy=eager package_name

Think before you leap

Prior to updating, check the landing ground - dependencies and version requirements.

After the huge leap (update), test the app to ensure it’s not broken. If it’s beyond repair, consider the sweet pain of breakup: uninstall.

Got an error about access denied to /usr/share/pyshared? Know where your package lives (--target).

Upgrading bulk packages & virtual environments

With great power comes great responsibility. System-wide updates aren't always the best. Consider a virtual environment for an isolated upgrade:

# Time for some 'me' space! python -m venv myenv source myenv/bin/activate # Happy upgrades inside my bubble pip install -U package_name

For bulk upgrades, there's nothing like the magic wand of a requirements file:

# The community of upgrades, coming together... pip install -r requirements.txt