The most comprehensive pip usage guide, 50% you may not have used it~

All Python developers know that the reason why Python is so popular that it can stand out among many high-level languages ​​is not only simple syntax and easy to use, but also due to the completeness of the Python ecology. There are tens of thousands of Pythons. Enthusiasts are willing to encapsulate various third-party toolkits that are beneficial to development based on Python.

This is how we can develop a project that meets basic needs as quickly as possible, instead of repeating the wheel every time.

It has been 28 years since Python was born in 1991, during which tens of thousands of third-party packages have been produced, and each package will be constantly updated, with more and more versions.

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

pip is an officially recommended package management tool. In the eyes of most developers, pip is almost standard for Python.

Of course there are other package management tools

  • distutils : only used for packaging and installation, strictly speaking not a package management tool

  • setuptools : An enhanced version of distutils, which extends distutils, provides more functions, introduces package dependency management, easy_install is one of its command line tools, and introduces the egg file format.

  • Pipenv : A tool that integrates dependency package management (pip) and virtual environment management (virtualenv)

  • There are others, which are not listed here.

Today's protagonist is pip, everyone is certainly not unfamiliar. But I believe that many people are only familiar with a few commonly used usages, and few other low-frequency and practical usages, but they know very little about them. In the past two days, I checked the official documents and sorted out these usages. It should be the Internet A more comprehensive introduction on.

1. Query software package

Query all packages installed in the current environment

$ pip list

Query packages with a certain 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 the package to the local without installing the package

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

After downloading, it is always necessary to install. You can specify to install the software package in this directory instead of installing from pypi.

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

Of course, you also build the wheel file from the package you downloaded

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

3. Install the package

Use pip install <pkg>can be easily downloaded from the pypi search and install python package.

As follows

$ 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 Only install locally, not from pypi

# 前提你得保证你已经下载 pkg 包到 /local/wheels 目录下
$ pip install --no-index --find-links=/local/wheels pkg

3.2 Limited version for software package installation

The following three types restrict the version of a single python package

# 所安装的包的版本为 2.1.2
$ pip install pkg==2.1.2

# 所安装的包必须大于等于 2.1.2
$ pip install pkg>=2.1.2

# 所安装的包必须小于等于 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

# 导出依赖包列表
pip freeze >requirements.txt

# 从依赖包列表中安装
pip install -r requirements.txt

# 确保当前环境软件包的版本(并不确保安装)
pip install -c constraints.txt

3.3 Restriction to install without binary package

By default, the platform of the wheel package is the platform on which the pip download command is run, so the platform may not be compatible.

For example, pymongo-2.8-cp27-none-macosx_10_10_intel.whl obtained under MacOS 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 be compiled during installation, so it will take longer.

# 下载非二进制的包
$ pip download --no-binary=:all: pkg

# 安装非二进制的包
$ pip install pkg --no-binary

3.4 Specify the 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 installinstallation package, it will fail.

Faced with this situation, there are two ways:

  1. Download the offline package and copy it to the intranet machine to install
  2. Use a proxy server to forward requests

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

  • Complex steps, time-consuming and labor-intensive
  • Unable to deal with package dependencies

Here is a key introduction, the second method:

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

It’s a little troublesome to send and enter long parameters every time the installation package is installed. For this, you can write them into the configuration file:$HOME/.config/pip/pip.conf

For this path, a few 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及以后) 
  • If you don’t have this file on your machine, just create it yourself

How to configure, here is an example:

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

# 替换出自己的代理地址,格式为[user:passwd@]proxy.server:port
proxy=http://xxx.xxx.xxx.xxx:8080 

[install]
# 信任阿里云的镜像源,否则会有警告
trusted-host=mirrors.aliyun.com 

3.5 Install user private software packages

Many people may not know that the python installation package can be user-isolated.

If you have administrator rights, 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 the machine.

If there are different users on a machine, it is irresponsible and dangerous to selfishly install or upgrade a certain package in the global environment.

Faced with this situation, we wondered whether we could install a package for me alone?

Fortunately, there are.

There are two ways I can think of:

  1. Use virtual environment
  2. Install the package in the user's environment

Virtual environment, I have written a few articles before, so I won't go into it here.

Today’s focus is on the second method, teach you how to install user-private packages?

Command is very simple, just add --userparameters, pip will be installed in the current user ~/.local/lib/python3.x/site-packages, the other users are not affected python.

pip install --user pkg

Let me give you an example

# 在全局环境中未安装 requests
[root@localhost ~]# pip list | grep requests   
[root@localhost ~]# su - wangbm
[root@localhost ~]# 

# 由于用户环境继承自全局环境,这里也未安装
[wangbm@localhost ~]# pip list | grep requests 
[wangbm@localhost ~]# pip install --user requests  
[wangbm@localhost ~]# pip list | grep requests 
requests (2.22.0)
[wangbm@localhost ~]# 

# 从 Location 属性可发现 requests 只安装在当前用户环境中
[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

# 退出 wangbm 用户,在 root 用户环境中发现 requests 未安装
[root@localhost ~]$ pip list | grep requests
[root@localhost ~]$ 

When you are in a personal user environment, python will first retrieve whether this package has been installed in the current user environment when importing the package. If it is installed, it will be used first, and if it is not installed, the package in the global environment will be used.

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/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']
>>> 

3.6 Extend the timeout period

If the network condition is not very good, it will often fail due to ReadTimeout when installing certain packages.

In this case, usually just a few retry attempts.

But this will inevitably be troublesome. Is there a better solution?

Yes, you can extend the timeout period.

$ pip install --default-timeout=100 <packages>

4. Uninstall the package

Just one command, I won’t repeat it

$ pip uninstall pkg

5. Upgrade package

If you want to upgrade the existing python, it is essentially to download the latest version of the package from pypi, and then install it. Therefore, the upgrade is also used pip install, but one parameter is added --upgrade.

$ pip install --upgrade pkg

When upgrading, there is actually an option --upgrade-strategythat is not used very much , which is used to specify the upgrade strategy.

There are only two options:

  • eager : Upgrade all dependent packages
  • only-if-need: Only when the old version cannot adapt to the new parent dependency package will it be upgraded.

After pip 10.0 version, the default value of this option is only-if-need, so the following two writings are the same.

pip install --upgrade pkg1 
pip install --upgrade pkg1 --upgrade-strategy only-if-need

6. Configuration file

When using pip to install some packages, the official source of pip will be used by default, so network timeout failures are often reported.

Common solution is that when you install the package, use the -iparameter to specify a mirror image of domestic sources. But every time you specify it is very troublesome, and you have to type a long string of letters.

At this time, you can actually write this source into the pip configuration file. When installing in the future, it will be installed from the source you configured by default.

How to configure it? Where are the files?

Use win+rinput %APPDATA%into the user profile folder, see if there is a pip folder, if not create it.

Then into this folder, create a new pip.inifile, as follows

[global]
time-out=60
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host=tsinghua.edu.cn

The above almost contains all the common usage scenarios of pip. For convenience, I will organize it into a table.

Guess you like

Origin blog.csdn.net/weixin_36338224/article/details/109370067