[Turn] the most complete pip guide, you may not have used 50%

[Turn] the most complete pip guide, you may not have used 50%

All Python developers are aware, Python is so popular, many high-level languages ​​can come to the fore, in addition to simple grammar, easy to use outside, but also due to more complete ecological Python, there are tens of thousands of Python Python-based enthusiasts are willing to package a variety of third-party tools conducive to the development of the package.

That we were able to use the fastest speed the development of a project to meet basic needs, but not always repeat-create the wheel.

Python born from 1991 to the present, the past 28 years, during which it produced tens of thousands of third-party packages, and each package will be constantly updated, there will be more and more versions.

When you are in a complex project environment, the absence of a valid dependencies management programs, maintenance of the project will be a big problem.

pip is the official recommended package management tool, in the eyes of most developers, pip almost Python's standard.

Of course there are other package management tools

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

  • setuptools : distutils enhanced version extends the distutils, provide more functionality, the introduction of package dependencies management, easy_install is that it's a command-line tool, introduced egg file format.

  • Pipenv : a set of dependencies management (pip) and virtual environment management (virtualenv) tools

  • There are other, do not list them here.

Today's protagonist is a pip, I am sure you will not be unfamiliar. But I believe a lot of people, but few are familiar with common uses, and for several other low frequency and practical usage, but little is known about these two days, I have access to official documents, the use of these sorted out, the network should be the comparative presentation of the whole.

1. Inquiry Packages

Query all installed packages in the current environment

$ pip list

A package containing the name of the query pypi

$ pip search pkg

Query environment may upgrade the current package

$ pip list --outdated

Query details of a package of

$ pip show pkg

2. Download Package

Without the installation package download package to a local

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

Downloaded, I fear, are to be installed, you can specify the directory to install packages without installing from pypi.

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

Of course, you can also download the package from your own build file generation wheel

$ 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, we can also to add more parameters to achieve different effects.

3.1 from the local installation, without installing from pypi

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

3.2 limited edition package installation

The following are three versions of a single python package was bound

# 所安装的包的版本为 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 entire pack version of python environment

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

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

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

3.3 restrictions no binary installation packages

Because default platform wheel package is a platform for running pip download command, so the situation is not adapted to the platform may occur.

For example obtained under MacOS System pymongo-2.8-cp27-none-macosx_10_10_intel.whl can not be installed in linux_x86_64.

Using the following command tar.gz package is downloaded, the installation can be used directly pip install.

Than the wheel package, this package will be compiled at the time of installation, the time it takes longer.

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

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

3.4 specify a proxy server installed

When you're in a network environment, you can not be directly connected to the public network. This time you use pip installthe installation package, it will fail.

Faced with this situation, there are two ways:

  1. Copied to the offline download package installed in the machine within the network
  2. Use a proxy server forwards the request

The first approach, although feasible, but there are quite a few drawbacks

  • Step complicated, time-consuming and labor-intensive
  • We can not handle package dependencies

Here the focus to introduce the second method:

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

Each time the installation package to send input parameters long, somewhat cumbersome, for which you can write the configuration file:$HOME/.config/pip/pip.conf

For this path, to explain some of

  • Different operating systems, different path
# 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及以后) 
  • Without this file on your machine, you can create your own

How to configure, here for a sample:

[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 users install proprietary software package

Many people may not know, python installation package can be isolated from the user.

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 who have administrator privileges on the machine.

If a user on a machine not only as selfishly will install or upgrade a package in the global environment, it is irresponsible and dangerous practice.

Faced with this situation, whether we wanted to install a separate package for our use of it?

Fortunately, there really.

There are two ways I can think of:

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

Virtual environment, several articles written before, no longer speaks to expand here.

Today's focus is on the second method, to teach you how to install proprietary user package?

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

Give 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're in the individual user environment, python when conducting package will first retrieve whether the current user environment has been installed this package has been installed takes precedence, is not installed using the global environment package.

Verify the following:

>>> 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 Packages

It is a command, not repeat them

$ pip uninstall pkg

5. Upgrade Package

Want to upgrade existing python, its essence is to start on pypi download the latest version of the package, and then install it. So the upgrade is to use pip install, just to add a parameter --upgrade.

$ pip install --upgrade pkg

When the upgrade, there is also the option of a very used --upgrade-strategy, it is used to specify an upgrade strategy.

It's only two options:

  • eager : Upgrade all dependencies
  • only-if-need: Only when the old version does not fit the new parent dependencies, will be upgraded.

After pip version 10.0, the default value of this option is only-if-need, therefore following two written is exchanged in a.

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

Above includes almost all common usage scenarios pip for convenience, I will organize it into a table, if you want, you can focus on my public number (Python programming time), backstage reply "pip", you can get HD without the watermark image .

 

Guess you like

Origin www.cnblogs.com/langqi250/p/11809839.html