[OSX] 使用 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 <command> [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>           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>                Path to a verbose appending log. This log is inactive by default.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --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.
  --cert <path>               Path to alternate CA bundle.

 

    至此,你就可以使用 python 2.7.6 和 pip 了。

 

補充:Homebrew 不會覆蓋那些應該原生安裝在 OS X 中的文件。這就是說如果某個原生包有了更新,Homebrew 會通知你而不是自行覆蓋它。它也可以將包安裝在用戶自己的目錄下,如此一來你就不用使用 sudo 來進行安裝,這種安裝方式意味着所有訪問路徑對你來說都是有效的,因此可以擺脫對系統庫的依賴。另外 Homebrew 有一個活躍的社區,因此更新非常快。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM