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相关推荐

  1. pip(Python包管理工具)安装第三方库教程

    目录 1 python环境检查 2 pip库的常用命令 2.1 更新包 2.1.1 更新pip工具 2.1.2 更新三方库 2.2 安装包 2.2.1 在线安装 2.2.1.1 直接安装 2.2.1. ...

  2. Python包管理工具Distribute的安装

    Python包管理工具Distribute的安装 Python的包管理工具常见的有easy_install, setuptools, 还有pip, distribute,那麽这几个工具有什么关系呢,看 ...

  3. linuxpip安装python包_Windows+Linux安装Python包管理工具pip

    Windows+Linux安装Python包管理工具pip Windows安装Python包管理工具pip pip是一个Python包管理工具,主要是用于安装PyPI上的软件包,可以替代easy_in ...

  4. Pipenv – 超好用的 Python 包管理工具

    pipenv 是什么 pipenv 是 python 官方推荐的包管理工具,集成了 virtualenv.pyenv 和 pip 三者的功能于一身,类似于 php 中的 composer. 我们知道, ...

  5. Python 包管理工具解惑

    本文转载自http://zengrong.net/post/2169.htm,感谢原作者. 一.困惑 作为一个 Python 初学者,我在包管理上感到相当疑惑(嗯,是困惑).主要表现在下面几个方面: ...

  6. python 包管理工具poetry

    | python 包管理工具poetry的使用 poetry 是一个python管理虚拟环境和依赖的管理工具,poetry和pip 类似,另外还提供了打包和发布的功能. 官方文档 https://py ...

  7. 新一代 Python 包管理工具来了!

    作者:费弗里 来源:Python 大数据分析 " 阅读本文大概需要 4 分钟. " 1 简介 说起Python的包管理工具,大家第一时间想到的肯定是pip.conda等经典工具.但 ...

  8. python模块版本管理工具_【转】Python包管理工具

    Python 包管理工具解惑 本站文章除注明转载外,均为本站原创或者翻译. 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也尊重你的智商: 本站部分原创和翻译文章提供m ...

  9. Pipenv – 超好用的 Python 包管理工具 1

    pipenv 是什么 pipenv 是 python 官方推荐的包管理工具,集成了 virtualenv.pyenv 和 pip 三者的功能于一身,类似于 php 中的 composer. 我们知道, ...

最新文章

  1. Windows 8 图标前面的勾选
  2. POJ2709 染料贪心
  3. 机器人动力学建模实例:二连杆机械臂
  4. 使用Anemometer基于pt-query-digest将MySQL慢查询可视化
  5. Git Permission to fazhiyun86/Test.git denied to MarRoar
  6. 神经网络-损失函数-等高线理解
  7. WDS部署服务之五高级功能
  8. Android 系统(51)Android窗口机制
  9. 依赖倒转原则_Java设计模式的七大原则
  10. C++11中的std::function和std::bind
  11. Java商城秒杀系统的设计与实战视频教程(SpringBoot版)_汇总贴
  12. 如何在iPhone上安装Skype?
  13. 非线性思维 -- 大前研一
  14. 计算机连接网络是飞行模式怎么办,电脑wifi界面只有飞行模式怎么办
  15. python使用阿里云企业邮箱发送邮件
  16. numpy中的revel和flatten
  17. java18天map和线程
  18. c#中regex的命名空间_C# Regex类用法
  19. gsoc 任务_gsoc 2020与cern hsf暗物质和深度学习
  20. 整形变量的定义和使用

热门文章

  1. 贝叶斯神经网络计算核裂变碎片产额
  2. PaddlePaddle版Flappy-Bird—使用DQN算法实现游戏智能
  3. 【Spring Boot 分享】开源项目【8个】
  4. nginx反向代理和负载均衡
  5. java出现errors是什么错误_java中错误(error)和异常(exception)有什么主要区别?
  6. JAVA——[MySQLNonTransientConnectionException:Could not create connection to database server.]解决方案
  7. mysql router 8.0.11_MySQL Router8
  8. Quartz详解和使用CommandLineRunner在项目启动时初始化定时任务
  9. 在服务器上打包部署springboot+vue的项目(一)启动tomcat后打开浏览器点击登录无反应 页面空白问题
  10. netstat命令查看套接字信息