English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn how to use pip to install and manage Python packages.
pip is the standard software package manager for Python. We can use pip to install other packages that are not included in the Python standard library. For example,
pip install numpy
If we have installed pip on the system, this command will install the numpy library.
pip is pre-installed in Python 3.4or an earlier version. We can check if pip is installed by using the following command in the command console:
pip --version
If pip is already installed in the system, pip will display the corresponding version, for example:
pip 19.3.1 from C:\Python37\lib\site-packages\pip (python 3.7)
If we are using an older version of Python or if pip is not installed for other reasons, please follow the steps described in the link below:pip install
pip is a command-line program. After installation, add pip as a command that can be used with the command prompt.
The basic syntax of pip is:
pip <pip arguments>
In addition to the standard Python libraries, the Python community has also customized many packages for various development frameworks, tools, and libraries.
Most of these packages are officially hosted and released toPython Package Index (PyPI).pip allows us to download and install these packages.
The install command is used to install packages using pip. Let's take an example:
Assuming we want to install requests, a popular Python HTTP library. We can operate with the help of the following command.
pip install requests
Output result
Collecting requests Using cached https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl Collecting chardet<3.1.0,>=3.0.2 Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 Using cached https://files.pythonhosted.org/packages/b4/40/a9837291310ee1ccc242ceb6ebfd9eb21539649f193a7c8c86ba15b98539/urllib3-1.25.7-py2.py3-none-any.whl Collecting idna<2.9,>=2.5 Using cached https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl Collecting certifi>=2017.4.17 Downloading https://files.pythonhosted.org/packages/b9/63/df50cac98ea0d5b006c55a399c3bf1db9da7b5a24de7890bc9cfd5dd9e99/certifi-2019.11.28-py2.py3-none-any.whl (156kB) Installing collected packages: chardet, urllib3, idna, certifi, requests Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.8 requests-2.220 urllib3-1.25.7
Here, we can see that the install command has been used with pip, followed by the name of the software package we want to install (requests).
pip also installs all other dependencies required for this software package, such as chardet, urllib3and certifi.
If pip install is used in the minimum form, pip will download the latest version of the software package.
Sometimes, only specific versions are compatible with other programs. Therefore, we can define the version of the package in the following way:
pip install requests==2.21.0
Here, we have installed the requests library of2.21.0 version.
The pip list command can be used to list all available packages in the current Python environment.
pip list
Output result
Package Version ---------- ---------- certifi 2019.11.28 chardet 3.0.4 idna 2.8 pip 19.3.1 requests 2.22.0 setuptools 45.0.0 urllib3 1.25.7 wheel 0.33.6
The pip show command displays information about one or more installed packages. Let's look at an example:
pip show requests
Output result
Name: requests Version: 2.22.0 Summary: Python HTTP for Humans. Home-page: http://python-requests.org Author: Kenneth Reitz Author-email: [email protected] License: Apache 2.0 Location: c:\users\dell\desktop\venv\lib\site-packages Requires: certifi, chardet, urllib3, idna Required-by:
Here, the show command displays information about the requests library. Note the following in the output above:RequiresandRequired-bycolumn.
The 'Requires' column shows the dependencies that the requests library needs. And, the 'Required-The 'by' column shows the packages that require requests.
We can do this by uninstalling the package pip and using the pip uninstall command.
Assuming we want to remove the requests library from the current Python environment. We can do this in the following way:
pip uninstall requests
Output result
Uninstalling requests-2.22.0: Would remove: C:\Python37\lib\site-packages\requests-2.22.0.dist-info\* C:\Python37\lib\site-packages\requests\* Proceed (y/n)? y Successfully uninstalled requests-2.22.0
As we can see, the requests package is deleted after the final prompt.
Note:Even if the specified package is deleted, the packages installed as dependencies will not be deleted. In this case, the dependencies of the requests library (chardet, urllib3and certifi) will not be uninstalled.
If you need to remove the dependencies of the package, you can use the 'pip show' command to view the installed packages and manually remove them.
A file containing all package names can also be used for batch installation of Python packages.
Let's look at an example:
Assuming we have a filerequirements.txt,which includes the following entries:
numpy Pillow pygame
We can use a single command to install all these packages and their dependencies with 'pip'.
pip install -r requirements.txt
Output result
Collecting numpy Using cached https://files.pythonhosted.org/packages/a9/38/f6d6d8635d496d6b4ed5d8ca4b9f193d0edc59999c3a63779cbc38aa650f/numpy-1.18.1-cp37-cp37m-win_amd64.whl Collecting Pillow Using cached https://files.pythonhosted.org/packages/88/6b/66f502b5ea615f69433ae1e23ec786b2cdadbe41a5cfb1e1fabb4f9c6ce9/Pillow-7.0.0-cp37-cp37m-win_amd64.whl Collecting pygame Using cached https://files.pythonhosted.org/packages/ed/56/b63ab3724acff69f4080e54c4bc5f55d1fbdeeb19b92b70acf45e88a5908/pygame-1.9.6-cp37-cp37m-win_amd64.whl Installing collected packages: numpy, Pillow, pygame Successfully installed Pillow-7.0.0 numpy-1.18.1 pygame-1.9.6
Here, we use the same 'install' command for pip.
However, additional parameters-The 'r' specifies that we are passing a requirements file instead of a package name to pip.
As an alternative to manually creating a requirements file, pip provides the 'freeze' command. Let's see how to use this command.
Assuming our current Python environment has the following packages. They can be displayed using 'pip list'.
Package Version ---------- ------- numpy 1.17.0 Pillow 6.1.0 pip 19.3.1 pygame 1.9.6 setuptools 45.0.0 wheel 0.33.6
The 'freeze' command lists Python packages that are not pre-installed.
pip freeze
Output result
numpy==1.17.0 Pillow==6.1.0 pygame==1.9.6
The 'pip freeze' command displays the packages and their versions in the format of a requirements file.
Therefore, you can use the following command to redirect the output to create a requirements file:
pip freeze > requirements.txt
Create a new one in the working directoryrequirements.txtFile. It can be used in other Python environments in the future to install specific versions of packages.
The search command is used to search for packages in the command prompt. Let's look at an example:
pip search pygame
Output result
pygame-anisprite (1.0.0) - Animated sprites for PyGame! pygame-ai (0.1.2) - Videogame AI package for PyGame pygame-engine (0.0.6) - Simple pygame game engine. pygame-assets (0.1) - Assets manager for Pygame apps pygame-gui (0.4.2) - A GUI module for pygame 2 pygame-spritesheet (0.2.0) - Python pygame extension that provides SpriteSheet class. pygame-minesweeper (1.0) - Minesweeper game implemented in python using pygame pygame-menu (2.1.0) - A menu for pygame, simple, lightweight and easy to use pygame-plot (0.1) - Quick visualization of data using pygame with a matplotlib style pygame (1.9.6) - Python Game Development ...
Here, we searched for a library named pygame. Display all other software packages that match the keyword. This command helps to find related software packages.
To learn more information about pip, please visit:Python pip (official document)