pip的使用

首先,来熟悉几个概念

  1. PyPI – the Python Package Index · PyPI
    The Python Package Index (PyPI) is a repository of software for the Python programming language.
    Package authors use PyPI to distribute their software.
    PyPI是Python官方维护的软件仓库,包含众多Python使用的第三方插件。
    
  2. PyPA
    The Python Packaging Authority (PyPA) is a working group that maintains many of the relevant projects in Python packaging.
    PyPA是Python社区的一个官方团队,负责维护和Python软件打包相关的项目。
    

pip 是什么?

pip is a command line program. When you install pip, a pip command is added to your system.

pip是一个命令行程序,可以帮你安装、升级、卸载Python的第三包。

安装 pip

Windows pip安装 - 知乎

基本使用

安装包

从PyPI官方仓库安装

1
pip install SomePackage

命令行示例:

1
2
3
$ pip install SomePackage
[...]
Successfully installed SomePackage

从本地文件安装

通过已经下载好了 .whl 文件或者 .tar.gz 文件安装,在没有联网或者网速较慢的电脑上可以采用这种方式。

1
pip install SomePackage-1.0-py2.py3-none-any.whl
1
pip install SomePackage-1.0-py2.py3-none-any.tar.gz

命令行示例:

1
2
3
$ pip install SomePackage-1.0-py2.py3-none-any.whl
[...]
Successfully installed SomePackage
1
2
3
$ pip install SomePackage-1.0-py2.py3-none-any.tar.gz
[...]
Successfully installed SomePackage

不过,直接安装 .tar.gz 文件可能需要第三方包 wheel 的支持,还有待进一步确认。

查看哪些包有更新

1
pip list --outdated

命令行示例:

1
2
$ pip list --outdated
SomePackage (Current: 1.0 Latest: 2.0)

升级包

1
pip install --upgrade SomePackage

命令行示例:

1
2
3
4
5
6
7
$ pip install --upgrade SomePackage
[...]
Found existing installation: SomePackage 1.0
Uninstalling SomePackage:
Successfully uninstalled SomePackage
Running setup.py install for SomePackage
Successfully installed SomePackage

卸载包

1
pip uninstall SomePackage

命令行示例:

1
2
3
4
5
$ pip uninstall SomePackage
Uninstalling SomePackage:
/my/env/lib/pythonx.x/site-packages/somepackage
Proceed (y/n)? y
Successfully uninstalled SomePackage

升级pip

在Windows系统上:

1
python -m pip install -U pip

在Linux系统上:

1
pip install -U pip

进阶使用

在命令行打开的多种方式

一般情况下可以直接在命令行使用这个命令

1
pip

如果这个命令不能用,说明pip安装的文件夹不在系统环境变量 PATH 中,那么可以使用这种方式:

1
python -m pip

在Windows上,还可以使用这种方式。

1
py -m pip

查看pip版本

1
pip -V
1
pip --version

注意大小写。

使用PyPI镜像

清华大学开源软件镜像站

临时安装

比如,使用清华大学提供的PyPI镜像

1
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

其中的 -i 参数为 --index-url 的缩写

设为默认

升级 pip 到最新的版本 (>=10.0.0) 后进行配置:

1
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

修改回初始值(PyPI):

1
pip config set global.index-url https://pypi.python.org/simple

安装指定版本的包

1
2
3
$ pip install SomePackage            # latest version
$ pip install SomePackage==1.0.4 # specific version
$ pip install 'SomePackage>=1.0.4' # minimum version

通过 requirements.txt 安装

1
pip install -r requirements.txt

requirements.txt 文件格式

官方文档的说明:Requirements File Format

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
#### example-requirements.txt ####
#
### Requirements without Version Specifiers ###
nose
nose-cov
beautifulsoup4
#
### Requirements with Version Specifiers ###
# See https://www.python.org/dev/peps/pep-0440/#version-specifiers
docopt == 0.6.1 # Version Matching. Must be version 0.6.1
keyring >= 4.1.1 # Minimum version 4.1.1
coverage != 3.5 # Version Exclusion. Anything except version 3.5
Mopidy-Dirble ~= 1.1 # Compatible release. Same as >= 1.1, == 1.*
#
### Refer to other requirements files ###
-r other-requirements.txt
#
#
### A particular file ###
./downloads/numpy-1.9.2-cp34-none-win32.whl
http://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev1820+49a8884-cp34-none-win_amd64.whl
#
### Additional Requirements without Version Specifiers ###
# Same as 1st section, just here to show that you can put things in any order.
rejected
green
#

列出所有安装的包

1
pip list

搜索包

1
pip search package-name

参考资料

  1. pip — pip 18.0 documentation
  2. Pypi | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
  3. Windows pip安装 - 知乎