January Star
  • Home
  • Categories
  • Tags
  • Archives

pip指南

pip 官方文档

以下内容是根据 pip 的 1.5.X 版本的官方文档写的笔记。

官方文档有用户指南专门一章节,我翻译时将之分割到多个章节中去了。

Contents

  • 安装 pip
  • 升级 pip
  • 常用命令介绍
  • 配置
    • 配置文件
    • 环境变量
    • 配置的优先级
    • 命令的自动补全
  • pip install
    • 使用方法
    • 本地快速安装
    • 非递归式升级
    • 特定用户安装
  • pip uninstall
    • 使用方法
    • 已知缺陷
    • 参数
  • pip freeze
    • 使用方法
    • 参数
    • 常见应用场景
  • pip list
    • 使用方法
    • 参数
  • pip show
    • 使用方法
    • 参数
  • pip search
    • 使用方法
    • 参数
  • pip wheel
    • 使用方法
    • 参数
    • 常见应用场景
  • 其它
    • 国内镜像源

安装 pip

可以下载 get-pip.py , 运行命令:

1
$ python get-pip.py

如果 setuptools 没有安装,该命令同时也会安装它。

如果已经安装了 setuptools 库,那么可以通过 easy_install 安装:

1
$ easy_install pip

升级 pip

在 Linux 系统上:

1
$ pip insall -U pip

在 Windows 系统上:

1
$ python -m pip install -U pip

常用命令介绍

  • 安装包

    1
    $ pip install SomePackage
    
  • 升级包

    1
    2
    3
    $ pip install --upgrade SomePackage
    
    $ pip install -U SomePackage
    
  • 删除包

    1
    $ pip uninstall SomePackage
    
  • 查看某个包安装了哪些文件

    1
    $ pip show --files SomePackage
    
  • 列出哪些包过期了,或者说是有新的版本了

    1
    $ pip list --outdated
    

配置

配置文件

配置文件路径:

  • Unix: ~/.pip/pip.conf
  • Windows: %HOMEPATH%pippip.ini

当然,如果你想自定义配置文件的存放路径,你可以将你自定义的路径保存到环境变量 PIP_ONFIG_FILE 。

配置文件的格式类似于 INI 文件。

当你想添加一个对所有命令都生效的参数时,可以将该配置放置在 [global] 段中。例如:

1
2
3
[global]
timeout = 60
index-url = http://pypi.douban.com/simple

如果你想针对 pip 的某个子命令进行配置的话,可以将该子命令名称当作段名称,该段内的配置与 [global] 中的配置冲突则会覆盖之。

  • 比如 freeze 子命令:

    1
    2
    [freeze]
    timeout = 10
    
  • 比如 install 子命令:

    1
    2
    3
    4
    5
    6
    [install]
    ignore-installed = true
    no-dependencies = yes
    find-links =
           http://mirror1.example.com
           http://mirror2.example.com
    

环境变量

pip 的命令行参数也可以通过环境变量来设置。

它的格式为: PIP_<UPPER_LONG_NAME> 。

  • 比如设置超时:

    1
    $ export PIP_DEFAULT_TIMEOUT=60
    

    其效果等于:

    1
    $ pip --default-timeout=60 [...]
    
  • 比如设置 find-links:

    1
    $ export PIP_FIND_LINKS="http://mirror1.example.com http://mirror2.example.com"
    

    其效果等于:

    1
    2
    $ pip install --find-links=http://mirror1.example.com
    --find-links=http://mirror2.example.com
    

配置的优先级

刚才讲到一个参数的三种配置方法优先级顺序是:

  1. 命令行
  2. 环境变量
  3. 配置文件

命令的自动补全

用过 bash/zsh 环境的人都知道有自动补全功能。

pip 命令想要自动补全功能的话,可以运行以下命令:

1
2
3
$ pip completion --bash >> ~/.profile

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

加入到 profile 文件后,需要重新进入 bash/zsh 环境才能生效,你也可以运行以下命令,立即生效自动补全功能:

1
$ eval "`pip completion --bash`"

pip install

安装 Python 包

使用方法

1
2
3
4
5
6
7
8
9
$ pip install [options] <requirement specifier> ...

$ pip install [options] -r <requirements file> ...

$ pip install [options] [-e] <vcs project url> ...

$ pip install [options] [-e] <local project path> ...

$ pip install [options] <archive url/path> ...

本地快速安装

  1. 下载你所需要的所有的 Python 包
1
$ pip install --download <DIR> -r requirements.txt
  1. 使用以下命令即可安装刚才下载到本地的 Python 包
1
$ pip install --no-index --find-links=[file://]<DIR> -r requirements.txt

非递归式升级

其实就是只升级你需要的包,但是该包所依赖的包不安装 / 升级。

1
$ pip install --upgrade --no-deps SomePackage

特定用户安装

从 Python2.6 开始,Python 就支持将 Python 包安装到指定的用户目录了。

该用户的目录默认值是由 site.USER_BASE 所指定的。

如果想覆盖该值,则使用环境变量 PYTHONUSERBASE 。

可以通过 --user 参数来指定特定用户。

1
2
$ export PYTHONUSERBASE=/myappenv
$ pip install --user SomePackage

pip uninstall

卸载 Python 包

使用方法

1
2
3
$ pip uninstall [options] <package> ...

$ pip uninstall [options] -r <requirements file> ...

已知缺陷

目前已知的两种无法正常删除的情况:

  • 完全使用 disutils 模块制定的 Python 包,且通过 python setup.py install 来安装的。这种包没有什么元信息能够知道它倒底安装了哪些文件。
  • 通过 python setup.py develp 来安装的脚本。

参数

-r, --requirement <file>
  删除 requirements.txt 文件中的包含的 Python 包名,可以跟多个该参数
-y, --yes 在删除时不需要确认

pip freeze

将当前 Python 环境所有的安装包名输出成 requirements 格式。

使用方法

1
$ pip freeze [options]

参数

-r, --requirement <file>
 
  • 先输出该 requirement 文件内的 Python 包名。
  • 再输出当前环境安装的 Python 包,requirement 文件中有的 Python 包名则不再显示。
  • 当 requirement 文件中的包名在环境没有,则会给出提示。
-f, --find-links <url>
 

从该 URL 来查找 Python 包,查找出来的 Python 包名也会输出出来。

说实话,我还真不知道这个参数的应用场景是什么。

-l, --local 如果一个 virtualenv 环境被配置成能够读取全局的 Python 包,那么在该环境内运行 pip freeze -l 时,不会显示全局的 Python 包名。

常见应用场景

当你需要将一个 virtualenv 环境中复制到另外一个 virtualenv 环境时,你可以先在源 virtualenv 环境运行命令:

1
$ pip freeze > requirements.txt

然后再进入目的 virtualenv 环境运行命令:

1
$ pip install -r requirements.txt

这样就完成了虚拟环境的复制过程。

这时可能有人会问,virtualenv 环境不就是一个目录么,直接拷贝一下,不就一个跟原来一样的新的 virtualenv 环境么?

好吧,我觉得这方法一般情况也可以的。

但是如果源 virtualenv 环境和目的 virtualenv 环境的 Python 版本或者操作系统不一样,建议你还是老实地按照上面的说做吧。

pip list

列出当前环境所有已经安装的 Python 包,包括可编辑的包(including editables)。

好像功能和 pip freeze 功能差不多的么,只是输出的格式不一样。

可编辑的包是啥意思?暂时还不清楚。

使用方法

1
$ pip list [options]

参数

-o, --outdated 列出所有有新版本的 Python 包名(不包括可编辑包)
-u, --uptodate 列出所有更新到最新版本的 Python 包名
-e, --editable 列出所有可编辑的包名
-l, --local 如果一个 virtualenv 环境被配置成能够读取全局的 Python 包,那么在该环境内运行 pip list -l 时,不会显示全局的 Python 包名。
--pre 列出的包中包括预发行或者是开发包,默认只会列出稳定版本的包
-i, --index-url <url>
 Python Package Index 的 URL 地址
--extra-index-url <url>
  更多的 Python Package Index 的 URL 地址
--no-index 忽略包索引,只与 --find-links 参数配合使用
-f, --find-links <url>
 

如果为一个 URL 或者是一个指定 HTML 页面的路径,PIP 会从该地址解析出包地址。

如果是一个本地目录,PIP 就会直接从该目录中查找所需要的包。

--allow-external <package>
 Allow the installation of externally hosted files
--allow-all-external
 Allow the installation of all externally hosted files
--allow-unverified <package>
 Allow the installation of insecure and unverifiable files
--process-dependency-links
 Enable the processing of dependency links

pip show

列出一个或者多个包的信息

使用方法

1
$ pip show [options] <package> ...

参数

-f, --files 列出一个已安装包中的所有文件

pip search

从 Python Package Index 上查找 Python 包名或者其简要描述中包含关键字(<query>)的 Python 包。

使用方法

1
$ pip search [options] <query>

参数

--index <url> Python Package Index URL 地址

pip wheel

创建 wheel 格式的 Python 包

使用该参数,需要您的环境安装 setuptools >= 0.8,并且安装了 wheel。

pip wheel 使用 setuptools 扩展 bdist_wheel 来制作 wheel 格式的 Python 包。

如果想更多了解 wheel,请看 wheel 官方文档 。

使用方法

1
2
3
4
5
6
7
8
9
$ pip pip wheel [options] <requirement specifier> ...

$ pip wheel [options] -r <requirements file> ...

$ pip wheel [options] <vcs project url> ...

$ pip wheel [options] <local project path> ...

$ pip wheel [options] <archive url/path> ...

参数

-w, --wheel-dir <dir>
 wheel 格式 Python 包的存放目录,默认为 <cwd>/wheelhouse
--no-use-wheel 当在 Python Package Index 上查找包时不优先查找 wheel 格式的 Python 包
--build-option <options>
 setup.py bdist_wheel 提供的额外参数
-r, --requirement <file>
  从 requirements 文件进行安装,该参数可以跟多个
--download-cache <dir>
  从网上下载的原始 Python 包的临时存放目录
--no-deps 不安装某个包的依赖包
-b, --build <dir>
 build 目录,在 virtualenv 环境中默认值为 <venv path>/build , 在正常环境默认值为 <OS temp dir>/pip_build_<username>
--global-option <options>
 bdsit_wheel 命令所用到的一些全局参数
--pre 列出的包中包括预发行或者是开发包,默认只会列出稳定版本的包
--no-clean 不清除 build 目录
-i, --index-url <url>
 Python Package Index 的 URL 地址 ( 默认为 https://pypi.python.org/simple/)
--extra-index-url <url>
  更多的 Python Package Index 的 URL 地址
--no-index 忽略包索引,只与 --find-links 参数配合使用
-f, --find-links <url>
 

如果为一个 URL 或者是一个指定 HTML 页面的路径,PIP 会从该地址解析出包地址。

如果是一个本地目录,PIP 就会直接从该目录中查找所需要的包。

--allow-external <package>
 Allow the installation of externally hosted files
--allow-all-external
 Allow the installation of all externally hosted files
--allow-unverified <package>
 Allow the installation of insecure and unverifiable files
--process-dependency-links
 Enable the processing of dependency links.

常见应用场景

先将需要的所有第三方 Python 包打包到本地目录,然后再从本地目录直接安装之前已经打包好的 wheel 格式的 Python 包。

1
2
$ pip wheel --wheel-dir=/tmp/wheelhouse SomePackage
$ pip install --no-index --find-links=/tmp/wheelhouse SomePackage

其它

国内镜像源

  • 豆瓣 PyPI
  • 华中理工大学 PyPI
  • 山东理工大学 PyPI , 这个貌似用不了。
  • 中国科学技术大学 PyPI

可以用 pip 的 -i 参数来指定镜像源。

1
$ pip install gevent -i http://pypi.douban.com/simple

如果不想每次手动指定,且永久有效的话,可以写入到 pip 的配置文件中。

1
2
[global]
index-url = http://pypi.douban.com/simple
Comments
comments powered by Disqus

Published

Aug 28, 2014

Category

python

Tags

  • package 3
  • pip 1
  • python 23

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor