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:

MacPorts安装使用Python/pip
$ 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.

  >>> 

MacPorts安装使用Python/pip

  可以看到当前为Mavericks自带的Python 2.7.5。

 

2. 使用port search查找一下目标资源:

MacPorts安装使用Python/pip
$ 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.
MacPorts安装使用Python/pip

 

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进行查看。

MacPorts安装使用Python/pip
$ 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/pip

    MacPorts会对不同版本的同一个程序进行分组,每个版本都有一个专属的名称,通过指定组名和专属名称就可以启用特定版本的程序。

 

    让我们看一下python的分组:

MacPorts安装使用Python/pip
$ port select --list python
Available versions for python:
    none (active)
    python25-apple
    python26-apple
    python27
    python27-apple
MacPorts安装使用Python/pip

    现在我们启用刚安装的python27,这需要sudo权限:

MacPorts安装使用Python/pip
$ 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.
>>> 
MacPorts安装使用Python/pip

    可以看到第一次没有sudo权限,操作失败。设置成功后会显示python27处于激活状态。同理,启用pip:

MacPorts安装使用Python/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.
MacPorts安装使用Python/pip

 

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

 

补充:Homebrew不会覆盖那些应该原生安装在OS X中的文件。这就是说如果某个原生包有了更新,Homebrew会通知你而不是自行覆盖它。它也可以将包安装在用户自己的目录下,如此一来你就不用使用sudo来进行安装,这种安装方式意味着所有访问路径对你来说都是有效的,因此可以摆脱对系统库的依赖。另外Homebrew有一个活跃的社区,因此更新非常快。

 

 

MacPorts安装使用Python/pip,布布扣,bubuko.com

MacPorts安装使用Python/pip

上一篇:Java 入门基础


下一篇:java中的重写