All Python developers know that Python is so popular, which can stand out among many high-level languages. In addition to its simple syntax and easy to use, it is also due to the complete Python ecosystem. Tens of thousands of Python enthusiasts are willing to package various third-party toolkits that are conducive to development based on python.
This allows us to develop a project that meets our basic needs as quickly as possible, rather than building wheels every time.
It has been 28 years since Python was born in 1991. During this period, tens of thousands of third-party packages have been generated, and each package will be updated continuously, with more and more versions.
When you are in a complex project environment, without an effective dependency package management scheme, project maintenance will be a big problem.
pip is the officially recommended package management tool. In the eyes of most developers, pip is almost the standard configuration of Python.
Of course, there are other package management tools
- distutils: only used for packaging and installation, not package management tool strictly
- setuptools: the enhanced version of distutils extends distutils, provides more functions, and introduces package dependency management. Easy install is one of its command-line tools, and introduces the file format of egg.
- Pipenv: a tool for set dependency package management (pip) and virtual environment management (virtualenv)
- There are others, which are not listed here.
Today's protagonist is pip, you will certainly not be unfamiliar. But I believe that many people are only familiar with several commonly used usages, but they know little about other low-frequency and practical usages. These two days, I checked the official documents and sorted out these usages, which should be a complete introduction on the Internet.
1. Query software package
Query all packages installed in the current environment
$ pip list
Query package with a name on pypi
$ pip search pkg
Query the upgradeable packages in the current environment
$ pip list --outdated
Query the details of a package
$ pip show pkg
2. Download the software package
Download package to local without installing package
$ pip download --destination-directory /local/wheels -r requirements.txt
After downloading, it is always to be installed. You can specify to install the package in this directory instead of from pypi.
$ pip install --no-index --find-links=/local/wheels -r requirements.txt
Of course, you can build your own wheel file from the package you downloaded
$ pip install wheel $ pip wheel --wheel-dir=/local/wheels -r requirements.txt
3. Install the software package
Using PIP install < PKG > can easily search, download and install python packages from pypi.
As shown below
$ pip install requests
This is the basic format of the installation package, and we can add more parameters to it to achieve different effects.
3.1 is only installed locally, not from pypi
# You have to make sure that you have downloaded the pkg package to the / local/wheels directory $ pip install --no-index --find-links=/local/wheels pkg
3.2 package installation with limited version
The following three types constrain the version of a single python package
# The installed package is version 2.1.2 $ pip install pkg==2.1.2 # The installed package must be greater than or equal to 2.1.2 $ pip install pkg>=2.1.2 # Package installed must be less than or equal to 2.1.2 $ pip install pkg<=2.1.2
The following commands are used to manage / control the package version of the entire python environment
# Export dependent package list pip freeze >requirements.txt # Install from list of dependent packages pip install -r requirements.txt # Ensure the version of the current environment package (does not ensure installation) pip install -c constraints.txt
3.3 restrict binary package installation
By default, the platform of the wheel package is the platform running the pip download command, so there may be platform mismatch.
For example, pymongo-2.8-cp27-none-macosx ﹐ 10 ﹐ intel.wh obtained from MacOS system cannot be installed in Linux ﹐ x86 ﹐ 64.
Use the following command to download the tar.gz package, which can be installed directly using pip install.
Compared with the wheel package, this package will compile at installation time, so it will take longer.
# Download non binary packages $ pip download --no-binary=:all: pkg #Install non binary packages $ pip install pkg --no-binary
3.4 designated proxy server installation
When you are in an intranet environment, you cannot directly connect to the public network. At this time, if you use the pip install package, you will fail.
Faced with this situation, there are two ways:
- Download offline package copy and install in Intranet machine
- Forwarding requests using a proxy server
The first method, though feasible, has many disadvantages
- Complicated steps, time-consuming and labor-consuming
- Unable to handle package dependency
Here we focus on the second method:
$ pip install --proxy [user:passwd@]http_server_ip:port pkg
It's a bit troublesome to input long parameters every time you install the package, so you can write it to the configuration file: $HOME/.config/pip/pip.conf
For this path, explain several points
- Different operating systems have different paths
# Linux/Unix: /etc/pip.conf ~/.pip/pip.conf ~/.config/pip/pip.conf # Mac OSX: ~/Library/Application Support/pip/pip.conf ~/.pip/pip.conf /Library/Application Support/pip/pip.conf # Windows: %APPDATA%\pip\pip.ini %HOME%\pip\pip.ini C:\Documents and Settings\All Users\Application Data\PyPA\pip\pip.conf (Windows XP) C:\ProgramData\PyPA\pip\pip.conf (Windows 7 And later)
- If you do not have this file on your machine, you can create it yourself
Here is an example of how to configure:
[global] index-url = http://mirrors.aliyun.com/pypi/simple/ # Replace your own proxy address in the form of [user:passwd@]proxy.server:port proxy=http://xxx.xxx.xxx.xxx:8080 [install] # Trust alicloud's image source, otherwise there will be a warning trusted-host=mirrors.aliyun.com
3.5 install user private package
Many people may not know that python's installation package can be isolated by users.
If you have administrator privileges, you can install the package in the global environment. This package in the global environment can be used by all users with administrator rights on this machine.
If the users on a machine are more than the same, it is irresponsible and dangerous to install or upgrade a package in the global environment selfishly.
In the face of this situation, we would like to install the package for my own use?
Fortunately, it does.
There are two ways I can think of:
- Use virtual environment
- Install the package in the user's environment
Virtual environment, I have written several articles before, and I will not talk about it here.
Today's focus is on the second method, which teaches you how to install user private packages?
The command is also very simple. As long as the -- user parameter is added, pip will install it under ~ /. local/lib/python3.x/site-packages of the current user, while python of other users will not be affected.
pip install --user pkg
Let's give you an example
# requests are not installed in the global environment [root@localhost ~]# pip list | grep requests [root@localhost ~]# su - wangbm [root@localhost ~]# # Because the user environment inherits from the global environment, it is not installed here either [wangbm@localhost ~]# pip list | grep requests [wangbm@localhost ~]# pip install --user requests [wangbm@localhost ~]# pip list | grep requests requests (2.22.0) [wangbm@localhost ~]# # From the Location property, it can be found that requests are only installed in the current user environment [wangbm@ws_compute01 ~]$ pip show requests --- Metadata-Version: 2.1 Name: requests Version: 2.22.0 Summary: Python HTTP for Humans. Home-page: http://python-requests.org Author: Kenneth Reitz Author-email: firstname.lastname@example.org Installer: pip License: Apache 2.0 Location: /home/wangbm/.local/lib/python2.7/site-packages [wangbm@localhost ~]$ exit logout # Exit the wangbm user and find that the requests are not installed in the root user environment [root@localhost ~]$ pip list | grep requests [root@localhost ~]$
When you are in a personal user environment, python will first retrieve whether the package has been installed in the current user environment. If it has been installed, the package will be used first. If it has not been installed, the package in the global environment will be used.
Verify as follows:
>>> import sys >>> from pprint import pprint >>> pprint(sys.path) ['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/home/wangbm/.local/lib/python2.7/site-packages', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/pip-18.1-py2.7.egg', '/usr/lib/python2.7/site-packages/lockfile-0.12.2-py2.7.egg'] >>>
4. Uninstall the package
Just one order, no more details
$ pip uninstall pkg
5. Upgrade package
To upgrade the existing python, in essence, you need to download the latest version of the package from pypi before installing it. Therefore, pip install is also used for upgrading, only one parameter -- upgrade.
$ pip install --upgrade pkg
When upgrading, there is actually an option that is not often used -- upgrade strategy, which is used to specify the upgrade strategy.
It has only two options:
- eager: upgrade all dependent packages
- Only if need: upgrade only if the old version cannot adapt to the new parent dependency package.
After PIP version 10.0, the default value of this option is only if need, so the following two writing methods are consistent.
pip install --upgrade pkg1 pip install --upgrade pkg1 --upgrade-strategy only-if-need
The above contains almost all the common usage scenarios of pip. For convenience, I sort out it into a form. If you need it, you can pay attention to my public number (Python programming time), and the background reply to "pip" can get HD and no watermark pictures.