Updating python packages with pip

How to use pip (Install, update, uninstall packages)

pip is the package installer for Python, used to install, update, and uninstall various Python packages (libraries).

Install pip

When you install Python using the standard python.org installer, pip is automatically installed.

  • working in a virtual environment
  • using Python downloaded from python.org
  • using Python that has not been modified by a redistributor to remove ensurepip
    Installation — pip documentation v23.1.2

The official documentation also provides instructions for individually installing pip using ensurepip or get-pip.py .

However, if pip is not installed, setting up a new Python environment with the python.org installer is often the easiest approach, unless you have a specific need to use an older system.

On a Mac, if you install Python with Homebrew, pip is automatically installed alongside it.

In the Anaconda environment, conda is used for package management, rather than pip.

pip and pip2, pip3

In environments where Python2 and Python3 are both present, pip2 and pip3 commands may be available alongside pip .

pip2 manages Python2 packages, while pip3 manages Python3 packages. The general pip command is associated with either Python2 or Python3, depending on the environment.

Читайте также:  Feature php 3 https

For example, if pip is for Python2, packages installed with pip will not work with Python3.

The command usage is the same for pip , pip2 , and pip3 .

You can use the pip show command described next to check where each package has been installed.

Check installed packages: pip show

Use pip show to check the details of an installed package.

For example, pip itself is one of the packages, and you can see its details as follows. The details include information such as the license and dependencies.

$ pip show pip Name: pip Version: 18.1 Summary: The PyPA recommended tool for installing Python packages. Home-page: [https://pip.pypa.io/](https://pip.pypa.io/) Author: The pip developers Author-email: pypa-dev@groups.google.com License: MIT Location: /usr/local/lib/python2.7/site-packages Requires: Required-by: 

Location displays the actual installation path of the package.

In the example environment, the pip3 command is also available, and the following results are displayed with pip3 show .

$ pip3 show pip Name: pip Version: 18.1 Summary: The PyPA recommended tool for installing Python packages. Home-page: [https://pip.pypa.io/](https://pip.pypa.io/) Author: The pip developers Author-email: pypa-dev@groups.google.com License: MIT Location: /usr/local/lib/python3.7/site-packages Requires: Required-by: 

These results show that the pip command installs packages in . /python2.7/site-packages and the pip3 command installs packages in . /python3.7/site-packages .

Note that this is just the result of the example environment, and depending on the environment, pip may be a command for Python3.

List installed packages: pip list , pip freeze

You can list installed packages using pip list .

$ pip list Package Version ---------- ------- future 0.16.0 pip 18.1 setuptools 39.2.0 six 1.11.0 wheel 0.31.1 

It is also possible to change the output format to output only up-to-date packages, outdated packages, and packages that are not dependencies of other packages. See the following article for details.

A similar command, pip freeze , is also provided.

$ pip freeze future==0.16.0 six==1.11.0 

freeze does not output pip itself and packages for package management such as setuptools and wheel .

freeze is useful to create requirements.txt . See the following article.

Install a package: pip install

Use pip install to install a package.

If a package is registered in the Python Package Index (PyPI), you can specify its name and install the latest version.

Multiple packages can be installed at the same time.

You can also use == to specify a version, such as 1.0.0 .

See the following article about how to install multiple packages with the configuration file requirements.txt .

Install from local or GitHub

As described above, packages registered in PyPI can be installed simply by specifying their names.

If the latest or bug-fixed version is not yet registered in PyPI, you can install it from your local directory or GitHub repository.

If you want to install it from local, specify the path of the directory that contains setup.py .

You can also install it by specifying a .zip or .whl file with a compressed directory containing setup.py .

$ pip install path/to/zipfile.zip 

You can also install it from the Git repository.

$ pip install git+https://github.com//

You can specify a branch or tag by adding @ at the end.

For example, the version with the v2.15.0 tag of Requests can be installed as follows.

$ pip install git+https://github.com/requests/requests@v2.15.0 

The installation with git+ requires Git to be installed on your system, as it performs a git clone operation during the installation process.

On GitHub, each version of the repository can be downloaded as a zip file from the release page, allowing you to specify the zip URL directly. In this case, you do not need to have git installed on your system.

$ pip install https://github.com/requests/requests/archive/v2.15.0.zip 

Update a package: pip install —upgrade

To update installed packages to their latest versions, use the —upgrade or -U option with pip install .

$ pip install --upgrade $ pip install -U

Update pip itself

The pip itself is also managed by pip.

If pip is not the latest version, the following message will be displayed when running the pip command.

You are using pip version 18.0, however version 18.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. 

As the message says, you can update the pip itself with the following command.

For the pip2 and pip3 commands, only the first pip should be replaced with pip2 or pip3 .

Uninstall a package: pip uninstall

To remove a package, use pip uninstall .

Multiple packages can be uninstalled at the same time.

By default, the system will ask for confirmation before actually removing the package.

$ pip uninstall pyflakes Uninstalling pyflakes-2.0.0: - Would remove: - /usr/local/bin/pyflakes - /usr/local/lib/python2.7/site-packages/pyflakes-2.0.0.dist-info/* - /usr/local/lib/python2.7/site-packages/pyflakes/* Proceed (y/n)? 

To uninstall without a confirmation prompt, use the —yes or -y option.

$ pip uninstall --yes $ pip uninstall -y

Check for dependencies: pip check

You can use pip check to verify that installed packages have compatible dependencies.

$ pip check No broken requirements found. 

If a dependent package is not installed, or if it is installed but the version is not correct:

$ pip check pyramid 1.5.2 requires WebOb, which is not installed. $ pip check pyramid 1.5.2 has requirement WebOb>=1.3.1, but you have WebOb 0.8. 

If you see such a message, you should install the corresponding package with pip install or update it with pip install -U .

Источник

How to Upgrade Python Packages with Pip

When was the last that you updated Python packages installed via Pip? Most of the users tend to forget that those packages also need to be updated, as just updating the system repository is not going to work here.

So let’s take a moment and see how to update old Python packages with Pip.

How to use pip to upgrade Python packages

Pip (Pip Installs Packages) is a command line utility to manage python packages. You can think of this as how we use apt to manage packages in Ubuntu and Debian.

So let’s dive deep into how you can use this fab utility to manage everything related to Python packages.

1. List outdated packages

Listing the outdated packages is the best idea to plan how you want to update packages as not many want to update their entire library of packages at once and wants to be selective.

To list outdated packages of Python, you just have to pair pip command with list option and —outdated flag as shown:

outdated packages

2. Upgrade a specific package

Once you get the list of the packages that need to be updated, you can be selective as I mentioned earlier, and to update a specific package, you’ll need to follow the given command syntax:

pip install package_name -U

For example, I want to upgrade the package named anime-api to the most recent version, so I’ll be using the given command:

update anime api

3. Upgrade package to specific version

It is not necessary to use only the most recent version of the software (cough Debian cough) and if you are in need of using packages to a specific version that may or may not be the most recent software, can be done using the given command syntax:

So I want to update the package named xdg to version 5.1 which is one point release behind the most recent build so my command would be:

pip install --upgrade xdg==5.1

upgrade xdg to specific iteration

4. Upgrade every package using Pip

NOTE: I do not recommend upgrading every package at once as most of the time, the dependencies are too complex to be handled.

To upgrade every python package, you’d need to follow the given command:

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

upgrade everything

The above command utilizes xargs. First, it will grab the packages that are needed to be updated and then perform pip3 install -U command over each package.

And I used pip3 here instead of pip. In Ubuntu 22.04 and later, both pip and pip3 commands are available.

Wrapping Up

Upgrading everything at once has never been a good idea in the case of pip. And I found myself in a state of broken dependencies so make sure you know what you will have.

And if you have any queries, feel free to ask in the comments.

Источник

Оцените статью