In this post, we'll take a deep dive into Python's primary package management tool - Pip. The content covers the basic concepts, installation and configuration of Pip, the use of mirror sources in China, package management, relationship with virtual environment, advanced usage, and problem solving.
1 Introduction
Dependency management has become a very important task in modern software development practice. It ensures that we can rebuild our development environment anywhere, and also allows us to easily track and update the libraries our projects depend on. Python is one of the most popular programming languages in the world, with a wealth of libraries and frameworks, thanks to Python's powerful package management tool Pip.
The Importance of Python Package Managers
Python's package manager allows developers to download, install, update, and manage Python packages. These packages can be third-party libraries such as numpy and tensorflow, or modules or packages you develop yourself. The package manager simplifies the process of obtaining and managing these resources, allowing developers to focus more on development.
For example, we can use pip to install numpy, a popular Python library:
pip install numpy
After this command is executed, Pip will download the numpy library from the Python Package Index (PyPI) and install it in the current environment. If numpy is already installed, Pip can also be used to upgrade numpy to the latest version:
pip install --upgrade numpy
Why you need to know and use Pip
Understanding and using Pip proficiently is very important for any Python developer. The Python community has developed a large number of useful libraries and frameworks, and developers can easily download and use these libraries and frameworks through Pip. In addition, using Pip can help developers better manage their project dependencies, thus building and maintaining their applications more efficiently.
2. The basic concept of Pip
In order to deeply understand and use Pip effectively, we need to understand some basic concepts first.
What is Pip
Pip is a package manager for Python that allows you to install and manage additional libraries and dependencies not included in the Python standard library. Pip is an acronym for recursion, and its full name is "Pip Installs Packages" or "Pip Installs Python". In addition to Python, Pip can also be used with some other variants of Python, such as PyPy.
The main functions of Pip are as follows:
- Install Python packages
- Uninstall Python packages
- Upgrade Python packages
- View installed Python packages
For example, you can use the following command to install the requests library:
pip install requests
When you run this command, Pip will download and install the requests library from the Python Package Index (PyPI).
History and Development of Pip
Pip was originally released in 2008 to provide a unified interface for installing and managing Python packages. Before Pip, developers in the Python community used a variety of different tools and methods to manage packages, which resulted in a lot of confusion and duplication of effort.
Pip has now become one of the most important tools in the Python ecosystem. Almost all modern Python projects rely on Pip to manage their libraries and dependencies. Additionally, many important Python tools, such as virtualenv and pipenv, are built on top of Pip.
Comparison of Pip with other Python package management tools
While Pip is the most commonly used Python package manager, it's not the only option. For example, conda is also a very popular Python package manager, especially in the field of data science and machine learning. Conda can manage package versions and environments better than Pip, but its use is not as common as Pip.
The following command shows how conda installs the numpy library:
conda install numpy
Regardless of which package manager you choose, it's important to understand its strengths and limitations, and how to choose and use the right tool for your needs.
3. Pip installation and configuration
Although most modern Python distributions come with Pip pre-installed, in some cases, you may need to install it manually. This section will guide you on how to install and configure Pip on different operating systems.
Ways to Install Pip on Different Operating Systems
-
Install Pip on Windows
If you are using Python 3.4 or later, then Pip should already be pre-installed in your Python environment. You can verify that Pip is installed with the following command:
python -m pip --version
If Pip is not installed, you can install Pip with the following command:
python get-pip.py
-
Install Pip on Linux
In most Linux distributions, you can use a package manager to install Pip. For example, in Ubuntu, you can use the following command to install Pip:
sudo apt-get install python3-pip
On CentOS, you can install Pip with the following command:
sudo yum install python3-pip
-
Install Pip on macOS
On macOS, you can use Homebrew to install Pip:
brew install python3
This command will install Python3 and Pip.
Pip version check and upgrade
You can check the version of Pip with the following command:
pip --version
If you need to upgrade Pip, you can use the following command:
# On Linux or macOS
pip install --upgrade pip
# On Windows
python -m pip install --upgrade pip
Basic configuration of Pip
Pip's behavior can be adjusted through command-line options, environment variables, and configuration files. The most common configuration is to change the installation source of the package. For example, you can use the following command to specify the PyPI mirror source of Tsinghua University:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
In addition, you can also permanently change the default source of Pip through the configuration file, the specific method will be described in detail in the following chapters.
The following is the content of the "Multiple Mirror Sources in China and How to Use" section and its subunits:
4. Multiple mirror sources in China and how to use them
Due to network reasons, domestic users may encounter slow speed or connection failure when downloading packages from Python's official PyPI library. Fortunately, we have multiple domestic mirror sources that can be used, which can significantly increase the download speed and success rate. Below we list some commonly used image sources and show how to use them.
Commonly used domestic mirror sources
- Alibaba Cloud: https://mirrors.aliyun.com/pypi/simple/
- University of Science and Technology of China: https://pypi.mirrors.ustc.edu.cn/simple/
- Douban: https://pypi.douban.com/simple/
- Tsinghua University: https://pypi.tuna.tsinghua.edu.cn/simple/
- Huazhong University of Science and Technology: http://pypi.hustunique.com/
How to use mirror sources
The method of using the mirror source is very simple. When using pip install
the command, add -i
the option followed by the URL of the mirror source. For example, the following command shows how to use the mirror source of Tsinghua University to install the numpy library:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy
Permanently set the mirror source
If you want to use a mirror source permanently, you can do so by modifying the configuration file of Pip. The location of the configuration file depends on your operating system:
- On Unix and macOS the configuration file is:
$HOME/.pip/pip.conf
- On Windows the configuration file is:
%HOME%\pip\pip.ini
In the configuration file, you can add the following content to permanently set the mirror source of Tsinghua University:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
After setting up the mirror source, you can use pip install
the command as usual, and Pip will automatically download the package from the mirror source you set.
5. Use Pip for package management
Pip provides a series of commands that make package management very convenient. This section will introduce how to use these commands in detail.
Installation package
You can use pip install
the command to install packages. For example, the following command shows how to install the numpy library:
pip install numpy
You can also specify the version of the package to install. For example, the following command shows how to install numpy version 1.18.5:
pip install numpy==1.18.5
uninstall package
You can use pip uninstall
the command to uninstall packages. For example, the following command shows how to uninstall the numpy library:
pip uninstall numpy
View installed packages
You can use pip list
the command to view installed packages and their versions. For example, the following command shows how to view all installed packages:
pip list
You can also use pip show
the command to view details of a specific package. For example, the following command shows how to view the details of the numpy library:
pip show numpy
upgrade package
You can use pip install --upgrade
the command to upgrade packages. For example, the following command shows how to upgrade the numpy library:
pip install --upgrade numpy
Install environment-specific packages
Pip also supports creating virtual environments, and then installing and managing packages in this virtual environment. This feature is very useful because it avoids version conflicts of packages and dependencies. For example, you can use the following command to create a virtual environment named myenv and install the numpy library in this environment:
python3 -m venv myenv
source myenv/bin/activate
pip install numpy
In this way, you can create a separate virtual environment for each project, and then install and manage packages in this environment, thereby avoiding the problem of version conflicts.
6. Pip and virtual environments
The virtual environment can help us isolate the Python environment between different projects and avoid problems caused by inconsistent versions of packages and dependencies. This section will introduce how to use Pip and venv
to create and manage virtual environments.
Create a virtual environment
You can use venv
modules to create virtual environments. For example, the following command shows how to create a virtual environment called myenv:
python3 -m venv myenv
This command will create a folder named myenv in the current directory, which contains an independent Python environment.
Activate the virtual environment
After creating a virtual environment, you need to activate the environment in order to install and use packages in that environment. On Unix and macOS, you can activate a virtual environment with the following command:
source myenv/bin/activate
On Windows, you can activate a virtual environment with the following command:
myenv\Scripts\activate
Using Pip in a virtual environment
Once the virtual environment is activated, you can use Pip as usual to install and manage packages. These packages will only be available in the current virtual environment. For example, the following command shows how to install the numpy library in a virtual environment:
pip install numpy
Exit the virtual environment
When you are done working in the virtual environment, you can deactivate
exit the virtual environment with the command:
deactivate
This command will return you to the system's Python environment.
Delete the virtual environment
If you no longer need a virtual environment, you can simply delete the virtual environment folder to remove the environment:
rm -rf myenv
7. Advanced usage of Pip
In addition to conventional operations such as installing, uninstalling, and upgrading packages, Pip also provides some advanced usage, such as installing specific versions of packages, installing pre-release versions of packages, installing wheel files, etc. This section will introduce these advanced usage.
Install a specific version of a package
As we mentioned earlier, you can use pip install <package>==<version>
the command to install a specific version of a package. You can also use >=
the , <=
, >
, <
and !=
operators to specify the version of the package to install. For example, the following command shows how to install a version of the numpy library greater than 1.18.5:
pip install numpy>=1.18.5
Install pre-release packages
By default, Pip will only install officially released packages. But sometimes, you may want to install a pre-release version of a package, for example to test new features. You can use --pre
options to install pre-release packages. For example, the following command shows how to install a pre-release version of the numpy library:
pip install --pre numpy
Install the wheel file
A wheel file is a precompiled package distribution format that allows for faster package installation. You can use pip install
the command to install the wheel file. For example, the following command shows how to install a numpy-1.21.2-cp39-cp39-win_amd64.whl
wheel file named:
pip install numpy-1.21.2-cp39-cp39-win_amd64.whl
Install the package from the GitHub repository
In addition to installing packages from PyPI repositories, you can also install packages directly from GitHub repositories. You just need to provide the URL of the repository. For example, the following command shows how to install the numpy library from the GitHub repository:
pip install git+https://github.com/numpy/numpy.git
8. Pip problem solving
Although Pip is a very powerful and flexible tool, you may encounter some problems while using it. This section will introduce some common problems and their solutions.
Package installation failed
From time to time, you may encounter problems with package installation failures. This could be due to a number of reasons such as network issues, dependency issues, compatibility issues, etc. Usually, you can find out the cause of the problem by examining the error message. For example, if an error message says that a dependency cannot be found, you may need to install that dependency first.
pip install missing-package
package conflict
Sometimes, you may encounter package conflicts, usually because different packages depend on different versions of the same package. You can use pip check
the command to check for package conflicts. If there is a conflict, you may need to upgrade or downgrade some packages to resolve the conflict.
pip check
Package uninstallation failed
Occasionally, you may encounter issues where packages fail to uninstall. This could be because the package is in use, or you do not have permission to uninstall the package. You can try using pip uninstall -y <package>
command to force uninstall package.
pip uninstall -y problematic-package
Package version is outdated
Sometimes, you may run into issues with outdated package versions. This is usually because your version of Pip is too old to install newer versions of packages. You can use pip install --upgrade pip
the command to upgrade Pip.
pip install --upgrade pip
If it is helpful, please pay more attention to
the personal WeChat public account: [Python full perspective]
TeahLead_KrisChang, 10+ years of experience in the Internet and artificial intelligence industry, 10+ years of experience in technology and business team management, Tongji Software Engineering Bachelor, Fudan Engineering Management Master, Aliyun certified cloud service senior architect, head of AI product business with hundreds of millions of revenue.