文章目录
  1. 1. 前言
  2. 2. 安装
  3. 3. 升级
  4. 4. 卸载
  5. 5. 用法
    1. 5.1. 安装
    2. 5.2. 升级
    3. 5.3. 卸载
    4. 5.4. 其他
  6. 6. FQA
  7. 7. 参考资料
  8. 8. 文档信息

前言


pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes.

因为 macOS 系统中自带 Python 工具,同时也可能带有 pip 工具,为了避免冲突,我们不会对 pip 进行软连接,会使用 pip3 命令。

安装


通常在安装 Python 时会自带安装 pip,如果你想自己安装也可以,但是不推荐。

1
2
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

升级


在 macOS 和 Linux 上使用:

1
2
3
4
5
pip install -U pip
or
pip install --upgrade pip
or
pip3 install --upgrade pip

通过 Python 升级:

1
/usr/bin/python3 -m pip install --upgrade pip

在 Windows 上使用:

1
python -m pip install -U pip

卸载


待研究

用法


下面将介绍比较常用的 pip 命令,比如:安装、升级、卸载等,你可以通过pip3 help 查看更多的用法:

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
Usage:   
pip3 <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.
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.
--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

安装


用法 pip3 install <package>,比如:使用 pip3 安装 gunicorn,将会安装最新包:

1
pip3 install gunicorn

安装特定版本的包:

1
pip3 install Flask-RESTful==0.3.7

由于网络问题,有时在连接或网速方面 pip 自带的官方源时可能会有些问题,推荐使用国内 PyPI(Python Package Index) 镜像源:

1
2
3
4
5
6
7
8
9
10
#腾讯云
http://mirrors.tencentyun.com/pypi/simple
#阿里云
https://mirrors.aliyun.com/pypi/simple/
#豆瓣(douban)
https://pypi.doubanio.com/simple/
#清华大学
https://pypi.tuna.tsinghua.edu.cn/simple/
#中国科技大学
http://pypi.mirrors.ustc.edu.cn/simple/

指定优先在某镜像源中进行查找并安装,比如指定豆瓣镜像源:

1
2
3
4
5
pip3 install --find-links=https://pypi.doubanio.com/simple/ flask

pip3 install -f https://pypi.doubanio.com/simple/ psycopg2

pip3 install --find-links=https://pypi.doubanio.com/simple/ --find-links=https://pypi.tuna.tsinghua.edu.cn/simple/ flask

将镜像源变量配置到当前终端中,只对某个终端起作用,然后再进行安装某包:

1
2
3
export PIP_FIND_LINKS="https://mirrors.aliyun.com/pypi/simple/"
or
export PIP_FIND_LINKS="https://mirrors.aliyun.com/pypi/simple/ https://pypi.doubanio.com/simple/"

升级


将软件包升级到最新版本:

1
pip3 install --upgrade requests

卸载


卸载其实很简单,可以使用下面命令:

1
2
3
pip3 uninstall Flask
or
pip3 uninstall Flask requests

其他


搜索包:

1
pip3 search appscript

显示已安装包的详情信息:

1
pip3 show gunicorn

将用到的库放到 requirements.txt 文件中:

1
$ pip3 freeze > requirements.txt

通过 requirements.txt 文件安装库:

1
$ pip3 install -r requirements.txt

FQA


Q: 若遇到这样类似的情况:socket.timeout: _ssl.c:1059: The handshake operation timed out or ERROR: No matching distribution found for Pillow or ERROR: Could not find a version that satisfies the requirement Pillow (from versions: none)

A: 请切换一下电脑网络类型。


Q: 安装依赖包时可能会遇到:zsh: no matches found: requests[socks]。
A: 在包的前后加上' 即可,eg:

1
pip3 install 'requests[socks]'

参考资料


文档信息


  • 版权声明:自由转载-保持署名-非商用-非衍生 ( CC BY-NC-ND 4.0 )
文章目录
  1. 1. 前言
  2. 2. 安装
  3. 3. 升级
  4. 4. 卸载
  5. 5. 用法
    1. 5.1. 安装
    2. 5.2. 升级
    3. 5.3. 卸载
    4. 5.4. 其他
  6. 6. FQA
  7. 7. 参考资料
  8. 8. 文档信息