设为首页 收藏本站
查看: 803|回复: 0

[经验分享] MacPorts安装使用Python/pip

[复制链接]
累计签到:77 天
连续签到:1 天
发表于 2014-4-20 20:08:16 | 显示全部楼层 |阅读模式
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有一个活跃的社区,因此更新非常快。






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-17910-1-1.html 上篇帖子: 【Python网络编程系列】打印你机器名字和IPV4地址 下篇帖子: python网络编程-使用socket实现S/C之间的tcp通信
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表