1.3. PythonMac OS X 上

Mac OS X 上,安装 Python 有两种选择:安装或不安装。您可能希望安装它。

Mac OS X 10.2 及更高版本预装了命令行版本的 Python。如果您熟悉命令行,则可以使用此版本学习本书的前三分之一内容。但是,预装版本没有附带 XML 解析器,因此当您学习到 XML 章节时,您需要安装完整版本。

您可能希望安装最新版本,而不是使用预装版本,最新版本还附带了一个图形交互式 shell。

步骤 1.3. 在 Mac OS X 上运行预装版本的 Python

要使用预装版本的 Python,请按照以下步骤操作

  1. 打开 /Applications 文件夹。

  2. 打开 Utilities 文件夹。

  3. 双击 Terminal 打开终端窗口并进入命令行。

  4. 在命令提示符下键入 python

试一试

Welcome to Darwin!
[localhost:~] you% python
Python 2.2 (#1, 07/14/02, 23:25:09)
[GCC Apple cpp-precomp 6.14] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>> [press Ctrl+D to get back to the command prompt]
[localhost:~] you% 

步骤 1.4. 在 Mac OS X 上安装最新版本的 Python

请按照以下步骤下载并安装最新版本的 Python

  1. http://homepages.cwi.nl/~jack/macpython/download.html 下载 MacPython-OSX 磁盘映像。

  2. 如果您的浏览器尚未执行此操作,请双击 MacPython-OSX-2.3-1.dmg 将磁盘映像挂载到桌面上。

  3. 双击安装程序 MacPython-OSX.pkg

  4. 安装程序将提示您输入管理员用户名和密码。

  5. 逐步执行安装程序。

  6. 安装完成后,关闭安装程序并打开 /Applications 文件夹。

  7. 打开 MacPython-2.3 文件夹

  8. 双击 PythonIDE 启动 Python

MacPython IDE 应显示一个启动画面,然后带您进入交互式 shell。如果交互式 shell 未出现,请选择 窗口->Python 交互式 (Cmd-0)。打开的窗口如下所示

Python 2.3 (#2, Jul 30 2003, 11:45:28)
[GCC 3.1 20020420 (prerelease)]
Type "copyright", "credits" or "license" for more information.
MacPython IDE 1.0.1
>>> 

请注意,安装最新版本后,预装版本仍然存在。如果您从命令行运行脚本,则需要注意使用的是哪个版本的 Python

示例 1.1. 两个版本的 Python

[localhost:~] you% python
Python 2.2 (#1, 07/14/02, 23:25:09)
[GCC Apple cpp-precomp 6.14] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>> [press Ctrl+D to get back to the command prompt]
[localhost:~] you% /usr/local/bin/python
Python 2.3 (#2, Jul 30 2003, 11:45:28)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>> [press Ctrl+D to get back to the command prompt]
[localhost:~] you%