w3resource

What is the process for upgrading and uninstalling packages using PIPin Python?

Upgrading and uninstalling with pip in Python

Using pip in Python, you can upgrade and uninstall packages. For both actions, here are the steps:

Upgrading Packages:

To upgrade a package to its latest version, you can use the '-upgrade' or '-U' flag with the 'pip install' command. For example:

pip install --upgrade package_name

or

pip install -U package_name

This command fetches and installs the latest version of the specified package, replacing the current version if it exists. If the package is not already installed, it will be installed as usual.

Uninstalling Packages:

To uninstall a package from your Python environment, use the "pip uninstall" command followed by the package name. For example,

pip uninstall package_name

This command removes the specified package and all its files from your Python environment.

Upgrade All Packages:

If you want to upgrade all installed packages to their latest versions, use the following command:

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

This command lists all the installed packages along with their versions using pip freeze. Then, it filters out any -e (editable) packages and extracts only the package names. Finally, it passes each package name to pip install -U, upgrading all installed packages to their latest versions.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-interview/what-is-the-process-for-upgrading-and-uninstalling-packages-using-pip-in-python.php