python pip 用法

pip 是 Python 包管理工具,该工具提供了对Python 包的查找、下载、安装、卸载的功能。

安装

1
2
3
4
# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py   # 下载安装脚本
# sudo python get-pip.py # 运行安装脚本

注意:用哪个版本的 Python 运行安装脚本,pip 就被关联到哪个版本

判断是否已安装

1
2
# pip --version     # Python2.x 版本命令
# pip3 --version # Python3.x 版本命令

常用命令

显示版本和路径

1
2
# pip --version
# pip -V

升级 pip

1
2
# pip install -U pip
# pip install --upgrade pip

安装包

1
2
3
4
5
# pip install SomePackage              # 最新版本
# pip install SomePackage==1.0.4 # 指定版本
# pip install 'SomePackage>=1.0.4' # 最小版本
# pip install 'SomePackage>=1,<2' # 版本在[1,2)
# pip install 'SomePackage~==1.0.4` # 要安装一个与特定版本“兼容”的版本

下载包

1
2
# pip download -d /path/package SomePackage             # 下载指定的包
# pip download -d /path/package -r requestments.txt # 下载requestments.txt文件中的包

安装离线包

1
2
3
# pip install --no-index --find-links=/path/package SomePackage              # 安装单个离线包
# pip install --no-index --find-links=/path/package -r requestments.txt # 批量安装离线包
# pip install --no-index --find-links=/path/package /path/package/* # 安装目录下的所有包

升级包

1
# pip install --upgrade SomePackage

卸载包

1
# pip uninstall SomePackage

搜索包

1
# pip search SomePackage

查看指定包的详细信息

1
# pip show [-f] SomePackage

列出已安装的包

1
# pip list

查看可升级的包

1
2
# pip list -o
# pip list --outdated

查看某个包有哪些版本

1
# pip install SomePackage==

镜像源

临时使用:

1
# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple SomePackage

详细用法

参考:https://pip.pypa.io/en/latest/reference/

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(ecloud_env) [root@i-2ld7xfi6 ~]# pip --help

Usage:
pip <command> [options]

Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip's wheel cache.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.

General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--no-input Disable prompting for input.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host <hostname> Mark this host or host:port pair as trusted, even though it does not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.
--no-color Suppress colored output
--no-python-version-warning
Silence deprecation warnings for upcoming unsupported Pythons.
--use-feature <feature> Enable new functionality, that may be backward incompatible.
--use-deprecated <feature> Enable deprecated functionality, that will be removed in the future.

install — Install packages

download — Download packages

uninstall — Uninstall packages

freeze — Output installed packages in requirements format

list — List installed packages

show — Show information about installed packages

check — Verify installed packages have compatible dependencies

有依赖项都是兼容的

1
2
3
4
5
# python -m pip check
No broken requirements found.

# echo $?
0

有未安装的包

1
2
3
4
5
# python -m pip check
pyramid 1.5.2 requires WebOb, which is not installed.

# echo $?
1

包的版本错误

1
2
3
4
5
# python -m pip check
pyramid 1.5.2 has requirement WebOb>=1.3.1, but you have WebOb 0.8.

# echo $?
1

config — Manage local and global configuration

search — Search PyPI for packages

cache — Inspect and manage pip’s wheel cache

wheel — Build wheels from your requirements

hash — Compute hashes of package archives

completion — A helper command used for command completion

debug — Show information useful for debugging

help — Show help for commands

-------------本文结束感谢您的阅读-------------