python包管理工具 ports_MacPorts安装使用Python/pip
Mac OS未預裝任何在Unix/Linux中常見的命令行包管理工具,Mac OS中的App Store和自身的軟件升級功能可以下載更新許多比較好的應用,但這些應用多數是滿足普通消費者需求,對于開發人員而言,命令行包管理工具才是王道。
Mac OS中主要有三種包管理工具:MacPorts,Homebrew,Fink。
這里簡要說一下三者區別:
1. MacPorts:因為Mac OS源自BSD,因此將BSD中的Port移植到OS成為MacPorts就一點不奇怪。MacPorts會獨立于OS自建一套倉庫樹,OS的包執行目錄為/usr/local,而MacPorts的包執行目錄為/opt/local。MacPorts盡量不影響系統現有程序,自然也就盡量少復用系統已有程序。MacPorts安裝包的過程是下載該程序的所有源文件及其依賴包,然后在本地進行Build生成最終的可執行文件。這樣做的好處是在系統中存在一套獨立的生態環境,從而不影響系統自身的生態環境,缺點就是浪費了系統自身的生態資源。
2. Homebrew:在Lion之后興起的包管理工具,工作方式與MacPorts類似,下載源代碼然后本地Build。不同之處在于Homebrew采取與MacPorts截然相反的態度,對系統資源是最大程度的利用,因此可以說Homebrew是直接對現有系統生態環境進行修改,所有包也是直接安裝到現有的生態環境中,這樣做的好處是節約了系統資源,但缺點是每一次操作都是直接對系統進行修改,嚴重依賴和影響現有系統。
3. Fink:基于Debian的packaging tools,它下載的都是已編譯后的二進制可執行文件(或源碼),因此無需本地編譯,即下即用。缺點是所有包都需要Fink預先編譯,導致更新速度跟不上。
使用MacPorts和Homebrew都需要預裝XCode Command Line Tools,即意味著還需要提前預裝XCode。(根據官方文檔來看,Fink安裝比較新的工具包時,因為提前編譯完成所以無需依賴XCode,但對于一些不是二進制的包,如果需要本地編譯,還是需要依賴XCode。換而言之,如果想在OS中編譯源碼,都需要XCode提供支持。)
正題:如何使用MacPorts安裝并使用Python和pip?
環境:Mac OS X Mavericks
我的Mavericks自帶Python版本有python25, python26, python27。本人目前使用的是Python 2.7.6,Mavericks攜帶的是Python 2.7.5。
1. 檢查當前使用的是哪個Python:
$ which python
/usr/bin/python
$ whereis python
/usr/bin/python
$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
可以看到當前為Mavericks自帶的Python 2.7.5。
2. 使用port search查找一下目標資源:
$ port search python | grep python27
dbus-python27 @0.84.0 (devel, python)
gnome-python27-extras @2.25.3 (gnome, python)
ice-python27 @3.4.2_3 (devel, python)
protobuf-python27 @2.4.1_1 (devel)
python27 @2.7.6 (lang)
python27-doc @2.7.2_1 (lang)
subversion-python27bindings @1.8.8 (devel, python)
$ port search python | grep pip
Lightweight pipelining: using Python functions as pipeline jobs.
py-pip @1.5.4 (python, www)
py-pipedream @0.2.0_1 (python)
python module for easy communication over pipes
Plumbum (Latin for lead, which was used to create pipes back in the day) is a small yet feature-rich library for shell script-like programs in Python.
py24-pipedream @0.2.0_1 (python)
python module for easy communication over pipes
py25-pip @1.3.1_1 (python, www)
Lightweight pipelining: using Python functions as pipeline jobs.
py26-pip @1.5.4 (python, www)
Lightweight pipelining: using Python functions as pipeline jobs.
py27-pip @1.5.4 (python, www)
Plumbum (Latin for lead, which was used to create pipes back in the day) is a small yet feature-rich library for shell script-like programs in Python.
py31-pip @1.5.4 (python, www)
Lightweight pipelining: using Python functions as pipeline jobs.
py32-pip @1.5.4 (python, www)
Lightweight pipelining: using Python functions as pipeline jobs.
py33-pip @1.5.4 (python, www)
Plumbum (Latin for lead, which was used to create pipes back in the day) is a small yet feature-rich library for shell script-like programs in Python.
py34-pip @1.5.4 (python, www)
Plumbum (Latin for lead, which was used to create pipes back in the day) is a small yet feature-rich library for shell script-like programs in Python.
3. 選擇自己需要的Python版本和pip版本進行安裝。事實上在這里我略過了安裝python27 @2.7.6 (lang),直接安裝py27-pip @1.5.4 (python, www),因為之前說過,MacPorts會自動安裝所需的工具及其依賴包。因此在安裝py27-pip的過程中,會自動安裝python27。
$ sudo port install py27-pip
Password:
---> Computing dependencies for py27-pip
---> Cleaning py27-pip
---> Scanning binaries for linking errors: 100.0%
---> No broken files found.
到此就完成了Python 2.7.6和pip的安裝。注意:因在本文之前我已經裝過py27-pip,因此以上示例僅做示意,如果你未使用MacPorts安裝過py27-pip或python27,你看到的輸出內容應與此不同。另外如果你是首次使用MacPorts進行工具包的安裝,MacPorts會有一個初次build倉庫樹的過程,需耐心等待。
4. 如果此時使用which命令查看python版本,你依然看到的是系統自帶版本,而使用pip命令時會得到找不到該命令的提示。
$ which python
/usr/bin/python
這是因為你尚未設置默認啟用的python和pip文件。需要啟用某個版本的程序可以通過port select命令來查看和選擇,想了解select命令可以使用$man port進行查看。
$ man port
...
select
For a given group, selects a version to be the default by creating appropriate symbolic links. For instance, python might be linked to python2.6. Available select groups are installed as subdi-
rectories of ${prefix}/etc/select/. To list the available versions in a group, use --list. To see which version is currently selected for a group, use --show. To change the selected version for
a group, use --set.
For example:
port select --list python
port select --show gcc
port select --set gcc mp-gcc44
...
MacPorts會對不同版本的同一個程序進行分組,每個版本都有一個專屬的名稱,通過指定組名和專屬名稱就可以啟用特定版本的程序。
讓我們看一下python的分組:
$ port select --list python
Available versions for python:
none (active)
python25-apple
python26-apple
python27
python27-apple
現在我們啟用剛安裝的python27,這需要sudo權限:
$ port select --set python python27
Selecting 'python27' for 'python' failed: could not create new link "/opt/local/bin/python" pointing to "/opt/local/bin/python2.7": permission denied
$ sudo port select --set python python27
Password:
Selecting 'python27' for 'python' succeeded. 'python27' is now active.
$ port select --list python
Available versions for python:
none
python25-apple
python26-apple
python27 (active)
python27-apple
$ which python
/opt/local/bin/python
$ python
Python 2.7.6 (default, Nov 18 2013, 15:12:51)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
可以看到第一次沒有sudo權限,操作失敗。設置成功后會顯示python27處于激活狀態。同理,啟用pip:
$ port select --list pip
Available versions for pip:
none (active)
pip27
$ sudo port select --set pip pip27
Selecting 'pip27' for 'pip' succeeded. 'pip27' is now active.
$ port select --list pip
Available versions for pip:
none
pip27 (active)
$ pip
Usage:
pip [options]
Commands:
install Install packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
search Search PyPI for packages.
wheel Build wheels from your requirements.
zip DEPRECATED. Zip individual packages.
unzip DEPRECATED. Unzip individual packages.
bundle DEPRECATED. Create pybundles.
help Show help for commands.
General Options:
-h, --help Show help.
-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.
--log-file Path to a verbose non-appending log, that only logs failures. This log is active by default at /Users/Xavier/.pip/pip.log.
--log Path to a verbose appending log. This log is inactive by default.
--proxy Specify a proxy in the form [user:passwd@]proxy.server:port.
--timeout Set the socket timeout (default 15 seconds).
--exists-action Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
--cert Path to alternate CA bundle.
至此,你就可以使用python 2.7.6和pip了。
補充:Homebrew不會覆蓋那些應該原生安裝在OS X中的文件。這就是說如果某個原生包有了更新,Homebrew會通知你而不是自行覆蓋它。它也可以將包安裝在用戶自己的目錄下,如此一來你就不用使用sudo來進行安裝,這種安裝方式意味著所有訪問路徑對你來說都是有效的,因此可以擺脫對系統庫的依賴。另外Homebrew有一個活躍的社區,因此更新非常快。
總結
以上是生活随笔為你收集整理的python包管理工具 ports_MacPorts安装使用Python/pip的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: map比vo效率 mybatis_myb
- 下一篇: 手机闪存速度排行_一举夺魁2.0!魅族1