Python包管理工具pip的安装和使用

Python有两个著名的包管理工具easy_install.py和pip。在Python2.7的安装包中,easy_install.py是默认安装的,而pip需要我们手动安装。

pip安装:

1. 获取pip安装脚本

方法1:利用常用curl获取

$ curl https://bootstrap.pypa.io/get-pip.py >> get-pip.py

>>后面是指定获取的pip脚本的名字,也可以是curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

方法2:利用wget获取,先要安装wget

$ wget https://bootstrap.pypa.io/get-pip.py

2.安装pip

$ sudo python get-pip.py

备注:看网上还有利用easy_install安装pip,但是我尝试了并没有成功

$ sudo easy_install pip 

Password:
Searching for pip
Reading https://pypi.python.org/simple/pip/
Download error on https://pypi.python.org/simple/pip/: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) -- Some packages may not be found!
Couldn't find index page for 'pip' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on https://pypi.python.org/simple/: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) -- Some packages may not be found!
No local packages or download links found for pip
error: Could not find suitable distribution for Requirement.parse('pip')

原因是 Python.org sites 终止支持TLS1.0和1.1版本,TLS需要>=1.2

参考:https://stackoverflow.com/questions/49768770/not-able-to-install-python-packages-ssl-tlsv1-alert-protocol-version

pip更新到最新版本

$ pip install -U pip

$ pip install -U pip
Requirement already up-to-date: pip in /Library/Python/2.7/site-packages (10.0.1)

pip命令

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

主要命令:

  • install: 安装包
  • download: 下载包
  • uninstall: 卸载包
  • freeze: 以requirements的格式输出已安装的包
  • list: 列出已安装的包
  • show: 显示已安装包的信息
  • check: 验证已安装包是否有兼容依赖问题
  • search: 在PyPI(python package index)中搜索包
  • wheel: 从requirements中构建wheels
  • hash: 计算包的hash签名
  • completion: 命令补全
  • help: 帮助命令

所有命令中,最重要的两个命令是install和uninstall。

Pip Install

pip支持四种方式安装python包。

  1. PyPI上安装包
  2. 从服务器托管的包中安装(版本管理工具,比如git,svn,vcs)
  3. 本地工程/包存档路径

1、从PyPI上安装包

1.1安装包

从PyPI安装一个包

$ pip install packageName            # 安装最新版本
$ pip install packageName==1.0.4     # 安装指定版本
$ pip install 'packageName>=1.0.4'     # 指定安装的最小版本
1.2、安装一个包到用户区

安装一个全局区域的包,一般需要sudo权限。在mac系统上,即使是管理员也无法安装,自从OS X El Capitan及以后的版本包含了一套安全技术(System Integrity Protection简称为SIP)来防止恶意软件修改系统保护区域。具体可参考SIP。因此,我们有时候需要将PyPI包安装到用户区,这个时候,可以用.

$ pip install --user packageName
1.3升级某个安装包

pip在升级软件包之前会自动卸载旧的软件包。

$ pip install -U|--upgrade  packageName
1.4一次安装多个包

可以将所有需要安装的包放入一个requirements.txt文件中,然后可以一次安装。requirements.txt 文件的每一行都要表明安装的内容,而且尽量不要依赖文件中指定包的前后安装顺序。

$ pip install -r requirements.txt

2、从服务器托管的包中安装

从git安装

$ pip install --user  git+https://git.repo/some_pkg.git    

从svn安装

$ pip install --user  svn+svn://svn.repo/some_pkg 

从一个分支安装

$ pip install --user  git+https://git.repo/some_pkg.git@feature 

3、安装本地包

$ pip install  path/to/project       # 指定项目的目录路径

还有很多其他的包安装功能,但是上面的已经满足了大部分需求。其他的请参考文档

Pip Uninstall

pip uninstall可以卸载大部分的包,除了一下两种情况

使用python setup.py install安装的

使用python setup.py develop安装的

使用方式主要有两种:

卸载单个包

$ pip uninstall package-name

卸载多个包

$ pip uninstall -r requirements.txt

Pip Check

pip check用来验证已安装的包是否有兼容的依赖性问题。

$ pip check
matplotlib 1.3.1 requires tornado, which is not installed.
matplotlib 1.3.1 requires nose, which is not installed.

上面的结果说明matplotlib包有两个依赖包没有安装。

Pip Search

pip search用来搜索名字或者摘要中包含搜索关键字的PyPI包。

$ pip help search

Usage:   
  pip search [options] <query>

Description:
  Search for PyPI packages whose name or summary contains <query>.

Search Options:
  -i, --index <url>           Base URL of Python Package Index (default
                              https://pypi.org/pypi)

选项只有一个,就是指定PyPI的url,默认url为https://pypi.python.org/pypi

例如,search frida

$ pip search frida
frida-runner (0.2.0)             - A Stupid CLI script to run Frida on iOS or
                                   Android
frida-push (1.0.8)               - Wrapper tool to identify the remote device
                                   and push device specific frida-server
                                   binary.
frida (11.0.6)                   - Inject JavaScript to explore native apps on
                                   Windows, macOS, Linux, iOS, Android, and
                                   QNX
frida-android-adb (1.2.0)        - A pure python implementation of the Android
                                   ADB and Fastboot protocols
frida-android-M2Crypto (0.27.0)  - M2Crypto: A Python crypto and SSL toolkit

以下两者都可以用,结果同上。

$ pip search -i https://pypi.org/pypi frida
$ pip search frida -i https://pypi.org/pypi

Pip List

pip list命令会按照字典序排列列举已安装的包,包括可编辑的包。

主要有如下选项:

  • -o, –outdated: 输出过时的包
  • -u, –uptodate: 输出最新的包
  • -e, –editable: 输出可编辑的工程
  • -l, –local: 如果虚拟环境有全局访问权限,不会列举全局安装的包
  • –user : 仅输出用户区安装的包
  • –pre : 包括预发版以及开发版本的包,默认情况仅输出稳定版本的包
  • –format : 输出格式,有4中,legacy,columns,freeze,以及json。 默认情况是legacy
  • –not-required :列举不被依赖的包

输出格式:

legacy:将要被废弃

$ pip list --format=legacy
DEPRECATION: The legacy format has been deprecated and will be removed in the future.
altgraph (0.10.2)
backports-abc (0.5)
bdist-mpkg (0.5.0)

freeze

$ pip list --format=freeze
altgraph==0.10.2
backports-abc==0.5
bdist-mpkg==0.5.0
bonjour-py==0.3
certifi==2018.1.18

columns

$ pip list --format=columns
Package                                Version  
-------------------------------------- ---------
altgraph                               0.10.2   
backports-abc                          0.5      
bdist-mpkg                             0.5.0    
bonjour-py                             0.3      
certifi                                2018.1.18

json

$ pip list --format=json
[{"version": "0.10.2", "name": "altgraph"}, {"version": "0.5", "name": "backports-abc"}, {"version": "0.5.0", "name": "bdist-mpkg"}

Pip Freeze

当某些时候debug的时候,需要提供一个完整的python环境,python freeze提供了此功能,它能够输出机器上python环境的快照(所有已安装的包)。

下面是freeze命令的选项:

  • -r, –requirement : 使用给定requirements文件的顺序和注释,此选项可多次使用
  • -f, –find-links : 用来发现包
  • -l, –local: 在虚拟环境中,将不输出全局安装的包
  • –user:仅输出安装在用户区的包
  • –all: 输出所有的包,并且包括pip,setupttool, distribute, wheel

输出用户区安装的前5个安装包:

$ pip freeze --user | head -5
backports-abc==0.5
futures==3.2.0
singledispatch==3.4.0.3
tornado==5.0.2

Pip Show

pip show可以用来显示每个包的具体信息。show命令只有一个选项 -f,用来显示安装包的文件列表。

$ pip show -f|--files packageName

$ pip show tornado
Name: tornado
Version: 5.0.2
Summary: Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.
Home-page: http://www.tornadoweb.org/
Author: Facebook
Author-email: python-tornado@googlegroups.com
License: http://www.apache.org/licenses/LICENSE-2.0
Location: /Users/clf/Library/Python/2.7/lib/python/site-packages
Requires: futures, singledispatch, backports-abc
Required-by: matplotlib



$ pip show -f tornado
Name: tornado
Version: 5.0.2
Summary: Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.
Home-page: http://www.tornadoweb.org/
Author: Facebook
Author-email: python-tornado@googlegroups.com
License: http://www.apache.org/licenses/LICENSE-2.0
Location: /Users/clf/Library/Python/2.7/lib/python/site-packages
Requires: futures, singledispatch, backports-abc
Required-by: matplotlib
Files:
  tornado-5.0.2-py2.7.egg-info
  tornado-5.0.2-py2.7.egg-info/PKG-INFO
  tornado-5.0.2-py2.7.egg-info/SOURCES.txt
  tornado-5.0.2-py2.7.egg-info/dependency_links.txt
  tornado-5.0.2-py2.7.egg-info/requires.txt
  tornado-5.0.2-py2.7.egg-info/top_level.txt
  tornado/__init__.py
  tornado/__init__.pyc
  tornado/_locale_data.py
  tornado/_locale_data.pyc
  ...

更多命令请参考文档

更改Pip源

pip默认的index-url是 https://pypi.python.org/pypi/
为了提高速度,我们可以更改pip源为国内的阿里云源。更改方法如下:

创建配置文件

$ mkdir ~/.pip/

$ cd ~/.pip/

$ vim pip.conf

添加阿里源

pip.conf内容如下:

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容