Python有兩個著名的包管理工具easy_install.py和pip腺办。在Python2.7的安裝包中效览,easy_install.py是默認(rèn)安裝的示损,而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
備注:看網(wǎng)上還有利用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: 驗(yàn)證已安裝包是否有兼容依賴問題
- search: 在PyPI(python package index)中搜索包
- wheel: 從requirements中構(gòu)建wheels
- hash: 計(jì)算包的hash簽名
- completion: 命令補(bǔ)全
- help: 幫助命令
所有命令中汉柒,最重要的兩個命令是install和uninstall。
Pip Install
pip支持四種方式安裝python包责鳍。
- 從PyPI上安裝包
- 從服務(wù)器托管的包中安裝(版本管理工具碾褂,比如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、安裝一個包到用戶區(qū)
安裝一個全局區(qū)域的包恤溶,一般需要sudo權(quán)限乓诽。在mac系統(tǒng)上,即使是管理員也無法安裝咒程,自從OS X El Capitan及以后的版本包含了一套安全技術(shù)(System Integrity Protection簡稱為SIP)來防止惡意軟件修改系統(tǒng)保護(hù)區(qū)域鸠天。具體可參考SIP。因此帐姻,我們有時候需要將PyPI包安裝到用戶區(qū)稠集,這個時候,可以用.
$ pip install --user packageName
1.3升級某個安裝包
pip在升級軟件包之前會自動卸載舊的軟件包饥瓷。
$ pip install -U|--upgrade packageName
1.4一次安裝多個包
可以將所有需要安裝的包放入一個requirements.txt文件中剥纷,然后可以一次安裝。requirements.txt 文件的每一行都要表明安裝的內(nèi)容扛伍,而且盡量不要依賴文件中指定包的前后安裝順序筷畦。
$ pip install -r requirements.txt
2、從服務(wù)器托管的包中安裝
從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 # 指定項(xiàng)目的目錄路徑
還有很多其他的包安裝功能鳖宾,但是上面的已經(jīng)滿足了大部分需求。其他的請參考文檔逆航。
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用來驗(yàn)證已安裝的包是否有兼容的依賴性問題。
$ pip check
matplotlib 1.3.1 requires tornado, which is not installed.
matplotlib 1.3.1 requires nose, which is not installed.
上面的結(jié)果說明matplotlib包有兩個依賴包沒有安裝因俐。
Pip Search
pip search用來搜索名字或者摘要中包含搜索關(guān)鍵字的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)
選項(xiàng)只有一個,就是指定PyPI的url抹剩,默認(rèn)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
以下兩者都可以用,結(jié)果同上澳眷。
$ pip search -i https://pypi.org/pypi frida
$ pip search frida -i https://pypi.org/pypi
Pip List
pip list命令會按照字典序排列列舉已安裝的包胡嘿,包括可編輯的包。
主要有如下選項(xiàng):
- -o, –outdated: 輸出過時的包
- -u, –uptodate: 輸出最新的包
- -e, –editable: 輸出可編輯的工程
- -l, –local: 如果虛擬環(huán)境有全局訪問權(quán)限钳踊,不會列舉全局安裝的包
- –user : 僅輸出用戶區(qū)安裝的包
- –pre : 包括預(yù)發(fā)版以及開發(fā)版本的包衷敌,默認(rèn)情況僅輸出穩(wěn)定版本的包
- –format : 輸出格式勿侯,有4中,legacy缴罗,columns助琐,freeze,以及json面氓。 默認(rèn)情況是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
當(dāng)某些時候debug的時候兵钮,需要提供一個完整的python環(huán)境,python freeze提供了此功能侧但,它能夠輸出機(jī)器上python環(huán)境的快照(所有已安裝的包)矢空。
下面是freeze命令的選項(xiàng):
- -r, –requirement : 使用給定requirements文件的順序和注釋,此選項(xiàng)可多次使用
- -f, –find-links : 用來發(fā)現(xiàn)包
- -l, –local: 在虛擬環(huán)境中禀横,將不輸出全局安裝的包
- –user:僅輸出安裝在用戶區(qū)的包
- –all: 輸出所有的包屁药,并且包括pip,setupttool, distribute, wheel
輸出用戶區(qū)安裝的前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命令只有一個選項(xiàng) -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默認(rèn)的index-url是 https://pypi.python.org/pypi/,
為了提高速度趾娃,我們可以更改pip源為國內(nèi)的阿里云源缭嫡。更改方法如下:
創(chuàng)建配置文件
$ mkdir ~/.pip/
$ cd ~/.pip/
$ vim pip.conf
添加阿里源
pip.conf內(nèi)容如下:
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com