pip是一个用来安装和管理Python包的工具, 它是easy_install的替代品, 也是目前社区的主流工具。

第三方包主要分布在The Python Package Index (https://pypi.org) 官方的仓库(简称 PYPI), Github, Bitbucker等代码托管服务上。

1. pip安装

ubuntu下pip安装:

$ sudo apt-get install python-pip -y

centos下pip安装:

# yum install python-pip -y

pip脚本一键安装:

$ wget c- https://bootstrap.pypa.io/get-pip.py
# python get-pip.py
# ln -s /usr/local/python2.7/bin/pip /usr/bin/pip

系统自带的pip版本比较低, 可使用pip的自更新来升级:

$ sudo pip install pip -U -q    # -q表示静默安装, 减少过程输出

2. 检查pip版本

$ pip -V    # 或 pip --version
pip 9.0.1 from /home/xx/.local/lib/python2.7/site-packages (python 2.7)

3. pip用法

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.
  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.
  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 6000.0 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 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.

4. pip使用国内源

详见使用国内Pypi源加速Pypi包的安装

5. pip配合虚拟环境virtualenv或virtualenvwrapper使用

详见python安装virtualenv和virtualenvwrapper

6. pip常用命令

pip安装命令:

# pip install package_name

pip升级命令:

# pip install --ungrage package_name

# pip install -U package_name

pip卸载命令:

# pip uninstall package_name

pip自身升级:

# pip install -U pip

pip指定源安装其他软件包, 如指定豆瓣源安装软件包setuptools

# pip install setuptools -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

使用requirements格式输出已安装的包, 如下:

$ pip freeze
Babel==2.5.1
click==6.7
Flask==0.12.2
Flask-Admin==1.5.0
Flask-Babel==0.11.2
Flask-BabelEx==0.9.3
Flask-SQLAlchemy==2.3.2
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
pytz==2017.3
speaklater==1.3
SQLAlchemy==1.1.15
Werkzeug==0.12.2
WTForms==2.1

在实际开发中, 我们常常在不同的服务器运行相同的项目, 故需要相同的python环境, 所以我们将python环境的包以requirements格式输出到文件requirements.txt中去:

$ pip freeze > requirements.txt

7. pip高级用法

pip支持自动补全功能, 对于zsh用户非常友好。 zsh用户使用如下命令就可以支持自动补全了:

$ pip completion --zsh >> ~/.zprofile
$ source ~/.zprofile

或者在~/.zshrc里加一行:

eval "`pip completion --zsh`"

如果使用bash, 则应该执行如下命令:

$ pip completion --bash >> ~/.profile

现在输入”pip i“就会变成”pip install”, 自动补全了install这个子命令。

OK, Enjoy it~