All Python developers know that Python’s popularity among other high-level languages owes more to its complete ecosystem than to its simple syntax and ease of use. There are tens of thousands of Python enthusiasts who are willing to package various third-party toolkits based on Python for development purposes.

That’s why we were able to develop a program that met basic needs as quickly as possible, rather than reinventing the wheel every time.

In the 28 years since Python was born in 1991, there have been tens of thousands of third-party packages, each of which is constantly updated with more and more versions.

When you are in a complex project environment, without an effective dependency package management scheme, project maintenance can be a big problem.

PIP is the officially recommended package management tool, and in the eyes of most developers, PIP is almost standard for Python.

There are other package management tools as well

  • Distutils: Used for packaging and installation only, not really a package management tool

  • Setuptools: Enhanced version of Distutils, which extends Distutils to provide more features, introducing package-dependent management. Easy_install is one of its command-line tools, introducing the egg file format.

    # install easy_install pkg. TGZ # install easy_install pkg.egg # install PKG from pypiCopy the code
  • Pipenv: an integrated dependency package management (PIP) and virtual environment management (VirtualEnv) tool

  • There are others, which are not listed here.

Today’s protagonist is PIP, we certainly will not be unfamiliar. However, I believe that many people are only familiar with a few common usages, but know little about the other several low-frequency and practical usages. These two days, I consulted the official documents and sorted out these usages, which should be a relatively complete introduction on the Internet.

1. Query software packages

Example Query all software packages installed in the current environment

$ pip list
Copy the code

Query the package name on pypI

$ pip search pkg
Copy the code

Example Query the upgradable packages in the current environment

$ pip list --outdated
Copy the code

Query the details of a package

$ pip show pkg
Copy the code

2. Download the software package

This section describes how to download software packages to a local PC without installing them

$ pip download --destination-directory /local/wheels -r requirements.txt
Copy the code

You can specify this directory to install the package instead of pypI.

$ pip install --no-index --find-links=/local/wheels -r requirements.txt
Copy the code

Of course, you can also build your own wheel files from the packages you download

$ pip install wheel
$ pip wheel --wheel-dir=/local/wheels -r requirements.txt
Copy the code

3. Install the software package

PIP Install < PKG > makes it easy to search, download and install Python packages from pypi.

As shown below.

$ pip install requests
Copy the code

This is the basic format of the installation package, but we can also add more parameters to it to achieve different effects.

3.1 Install only from the local PC, not pYPI

#Make sure you have downloaded the PKG package to /localWheels/directory
$ pip install --no-index --find-links=/local/wheels pkg
Copy the code

3.2 Installing software Packages in certain Versions

The following three constraints apply to versions of individual Python packages

#The installed package is version 2.1.2
$PIP install PKG = = 2.1.2

#The installed package must be at least 2.1.2
$PIP install PKG > = 2.1.2

#Installed packages must be less than or equal to 2.1.2
$PIP install PKG < = 2.1.2
Copy the code

The following commands are used to manage/control package versions of the entire Python environment

#Export the dependency package list
pip freeze >requirements.txt

#Install from the dependency package list
pip install -r requirements.txt

#Ensure the version of the current environment package (not ensure installation)
pip install -c constraints.txt
Copy the code

3.3 Do not Use binary package installation

Because by default, the platform for the Wheel package is the platform on which the PIP Download command is run, platform mismatches can occur.

For example, pymongo-2.8-cp27-none-macOSX_10_10_intel. WHL cannot be installed on linux_x86_64.

Use the following command to download the tar.gz package, which can be installed directly using PIP install.

Compared to the Wheel package, which is compiled when installed, so it takes longer.

#Download non-binary packages
$ pip download --no-binary=:all: pkg

#  Install non-binary packages
$ pip install pkg --no-binary
Copy the code

3.4 Specifying proxy Server Installation

If you are in an Intranet environment, you cannot directly connect to the Internet. When you use the PIP Install package, it will fail.

There are two ways to deal with this situation:

  1. Download the offline package and copy it to the Intranet machine for installation
  2. Use a proxy server to forward requests

The first method, though feasible, has quite a few drawbacks

  • The steps are complicated and time-consuming
  • Unable to handle package dependencies

Here is the second method:

$ pip install --proxy [user:passwd@]http_server_ip:port pkg
Copy the code

Every time the installation package is long hair input parameters, is some trouble, so you can write to the configuration file: $HOME/config/PIP/PIP. Conf

A couple of things about this path

  • The paths are different for different operating systems
# 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.iniC:\Documents and Settings\All Users\Application Data\PyPA\pip\pip.conf (Windows XP) C:\ProgramData\PyPA\pip\pip.conf (Windows 7 and later)Copy the code
  • If you don’t have this file on your machine, create it yourself

Here is an example of how to configure:

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/ 

[user:passwd@]proxy.server:port [user:passwd@]proxy.server:port
proxy=http://xxx.xxx.xxx.xxx:8080 

[install]
# Trust the mirror source of Aliyun, otherwise there will be a warning
trusted-host=mirrors.aliyun.com 
Copy the code

3.5 Installing User Proprietary Software Packages

Many people may not be aware of the fact that python installation packages are user-isolated.

If you have administrator privileges, you can install packages in the global environment. The package in the global environment can be used by all users with administrator privileges on the machine.

It is irresponsible and dangerous to selfishly install or upgrade a package in a global environment if there are more than one user on one machine.

Faced with this, we wondered if we could install a separate package for our own use.

Fortunately, there is.

I can think of two ways to do this:

  1. Using virtual Environments
  2. Install the package in the user’s environment

Virtual environments, which I have written several articles about, will not be covered here.

Today’s focus is on the second method, how to install the user’s private package?

Command is also very simple, as long as plus – user parameters, PIP will install it in the current user’s ~ /. Local/lib/python3. X/site – packages, and other users of python is not be affected.

pip install --user pkg
Copy the code

Let’s take an example

#Requests were not installed in the global environment
[root@localhost ~]# pip list | grep requests   
[root@localhost ~]# su - wangbm
[root@localhost ~]# 

#Since 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 ~ #
#You can see from the Location attribute that Requests is installed only 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: [email protected] 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 Requests is not installed in the root user environment
[root@localhost ~]$ pip list | grep requests
[root@localhost ~]$ 
Copy the code

When you are in a personal user environment, Python will first check whether the package is installed in the current user environment. If it is installed, the package will be used first. If it is not installed, the package will be used in the global environment.

The verification is as follows:

>>> import sys
>>> from pprint import pprint 
>>> pprint(sys.path)
[' '.'/usr/lib64/python27.zip'.'/ usr/lib64 / python2.7'.'/ usr/lib64 / python2.7 / platt - 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']
>>> 

Copy the code

4. Uninstall the software package

Just one order. I won’t repeat it

$ pip uninstall pkg
Copy the code

5. Upgrade the software package

To upgrade an existing Python, you essentially download the latest version of the package from Pypi and install it. PIP install is also used for the upgrade, but with one parameter –upgrade.

$ pip install --upgrade pkg
Copy the code

During the upgrade, there is a less used option called upgrade-strategy, which specifies the upgrade strategy.

There are only two options:

  • eager: Upgrade all dependency packages
  • only-if-need: Upgrade only if the old version does not fit the new parent dependency package.

After PIP 10.0, the default value for this option is only-if-need, so the following two versions are reciprocal.

pip install --upgrade pkg1 
pip install --upgrade pkg1 --upgrade-strategy only-if-need
Copy the code

The above almost includes all the commonly used scenarios of PIP. For convenience, I have organized them into a table. If you need, you can follow my official account and reply “PIP” in the background to obtain hd watermarked pictures.