Python有两个著名的包管理工具easy_install.py和pip。在Python2.7的安装包中,easy_install.py是默认安装的,而pip需要我们手动安装。
pip安装:
1. 获取pip安装脚本
方法1:利用常用curl获取
>>后面是指定获取的pip脚本的名字,也可以是curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
方法2:利用wget获取,先要安装wget
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
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包。
- 从PyPI上安装包
- 从服务器托管的包中安装(版本管理工具,比如git,svn,vcs)
- 本地工程/包存档路径
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