“Python/docs/3.9/using/mac”的版本间差异

来自菜鸟教程
Python/docs/3.9/using/mac
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
<div id="using-python-on-a-macintosh" class="section">
+
{{DISPLAYTITLE:4. 在 Mac 上使用 Python — Python 文档}}
 +
<div id="using-python-on-a-mac" class="section">
  
 
<span id="using-on-mac"></span>
 
<span id="using-on-mac"></span>
= <span class="section-number">4. </span>Using Python on a Macintosh =
+
= 4. 在 Mac 上使用 Python =
  
; Author
+
; 作者
: Bob Savage &lt;[mailto:bobsavage%40mac.com bobsavage<span>@</span>mac<span>.</span>com]&gt;
+
: 鲍勃·萨维奇&lt; [mailto:bobsavage%40mac.com bobsavage@mac.com] &gt;
  
Python on a Macintosh running Mac OS X is in principle very similar to Python on
+
运行 macOS 的 Mac 上的 Python 原则上与任何其他 Unix 平台上的 Python 非常相似,但还有许多附加功能,例如 IDE 和包管理器,值得一提。
any other Unix platform, but there are a number of additional features such as
 
the IDE and the Package Manager that are worth pointing out.
 
  
 
<div id="getting-and-installing-macpython" class="section">
 
<div id="getting-and-installing-macpython" class="section">
  
 
<span id="getting-osx"></span>
 
<span id="getting-osx"></span>
== <span class="section-number">4.1. </span>Getting and Installing MacPython ==
+
== 4.1. 获取和安装 MacPython ==
  
Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, you
+
10.8 版起的 macOS 随附 Apple 预装的 Python 2.7。 如果您愿意,我们邀请您从 Python 网站 (https://www.python.org) 安装最新版本的 Python 3。 一个当前的 Python 的“通用二进制”版本可以在 Mac 的新 Intel 和传统 PPC CPU 上本地运行,可以在那里获得。
are invited to install the most recent version of Python 3 from the Python
 
website (https://www.python.org). A current &quot;universal binary&quot; build of Python,
 
which runs natively on the Mac's new Intel and legacy PPC CPU's, is available
 
there.
 
  
What you get after installing is a number of things:
+
安装后你会得到很多东西:
  
* A <code>Python 3.9</code> folder in your <code>Applications</code> folder. In here you find IDLE, the development environment that is a standard part of official Python distributions; and PythonLauncher, which handles double-clicking Python scripts from the Finder.
+
* <code>Applications</code> 文件夹中的 <code>Python 3.9</code> 文件夹。 在这里您可以找到 IDLE,它是官方 Python 发行版的标准部分的开发环境; 和 PythonLauncher,它处理来自 Finder 的双击 Python 脚本。
* A framework <code>/Library/Frameworks/Python.framework</code>, which includes the Python executable and libraries. The installer adds this location to your shell path. To uninstall MacPython, you can simply remove these three things. A symlink to the Python executable is placed in /usr/local/bin/.
+
* 一个框架 <code>/Library/Frameworks/Python.framework</code>,其中包括 Python 可执行文件和库。 安装程序将此位置添加到您的 shell 路径。 要卸载 MacPython,您只需删除这三样东西即可。 Python 可执行文件的符号链接位于 /usr/local/bin/ 中。
  
The Apple-provided build of Python is installed in
+
Apple 提供的 Python 版本分别安装在 <code>/System/Library/Frameworks/Python.framework</code> <code>/usr/bin/python</code> 中。 您永远不应修改或删除这些内容,因为它们由 Apple 控制并由 Apple 或第三方软件使用。 请记住,如果您选择从 python.org 安装更新的 Python 版本,您的计算机上将有两个不同但功能强大的 Python 安装,因此您的路径和用法与您想要执行的操作一致非常重要。
<code>/System/Library/Frameworks/Python.framework</code> and <code>/usr/bin/python</code>,
 
respectively. You should never modify or delete these, as they are
 
Apple-controlled and are used by Apple- or third-party software. Remember that
 
if you choose to install a newer Python version from python.org, you will have
 
two different but functional Python installations on your computer, so it will
 
be important that your paths and usages are consistent with what you want to do.
 
  
IDLE includes a help menu that allows you to access Python documentation. If you
+
IDLE 包括一个帮助菜单,允许您访问 Python 文档。 如果您完全不熟悉 Python,您应该开始阅读该文档中的教程介绍。
are completely new to Python you should start reading the tutorial introduction
 
in that document.
 
  
If you are familiar with Python on other Unix platforms you should read the
+
如果您熟悉其他 Unix 平台上的 Python,您应该阅读有关从 Unix shell 运行 Python 脚本的部分。
section on running Python scripts from the Unix shell.
 
  
 
<div id="how-to-run-a-python-script" class="section">
 
<div id="how-to-run-a-python-script" class="section">
  
=== <span class="section-number">4.1.1. </span>How to run a Python script ===
+
=== 4.1.1. 如何运行 Python 脚本 ===
  
Your best way to get started with Python on Mac OS X is through the IDLE
+
在 macOS 上开始使用 Python 的最佳方法是通过 IDLE 集成开发环境,请参阅部分 [[#ide|IDE]] 并在 IDE 运行时使用“帮助”菜单。
integrated development environment, see section [[#ide|<span class="std std-ref">The IDE</span>]] and use the Help menu
 
when the IDE is running.
 
  
If you want to run Python scripts from the Terminal window command line or from
+
如果您想从终端窗口命令行或 Finder 运行 Python 脚本,您首先需要一个编辑器来创建您的脚本。 macOS 附带了许多标准的 Unix 命令行编辑器,其中包括 '''vim''' '''emacs'''。 如果您想要更像 Mac 的编辑器,可以使用 Bare Bones Software 的 '''BBEdit''' '''TextWrangler'''(请参阅 [http://www.barebones.com/products/bbedit/index.html http://www.barebones.com/products/bbedit/index.html)。 html]) 是不错的选择,'''TextMate''' 也是不错的选择(参见 https://macromates.com/)。 其他编辑器包括 '''Gvim''' (http://macvim-dev.github.io/macvim/) '''Aquamacs''' ([http://aquamacs.org/ http://aquamacs.组织/])。
the Finder you first need an editor to create your script. Mac OS X comes with a
 
number of standard Unix command line editors, '''vim''' and
 
'''emacs''' among them. If you want a more Mac-like editor,
 
'''BBEdit''' or '''TextWrangler''' from Bare Bones Software (see
 
http://www.barebones.com/products/bbedit/index.html) are good choices, as is
 
'''TextMate''' (see https://macromates.com/). Other editors include
 
'''Gvim''' (http://macvim-dev.github.io/macvim/) and '''Aquamacs'''
 
(http://aquamacs.org/).
 
  
To run your script from the Terminal window you must make sure that
+
要从终端窗口运行您的脚本,您必须确保 <code>/usr/local/bin</code> 在您的 shell 搜索路径中。
<code>/usr/local/bin</code> is in your shell search path.
 
  
To run your script from the Finder you have two options:
+
要从 Finder 运行脚本,您有两个选择:
  
* Drag it to '''PythonLauncher'''
+
* 将其拖到 '''PythonLauncher'''
* Select '''PythonLauncher''' as the default application to open your script (or any .py script) through the finder Info window and double-click it. '''PythonLauncher''' has various preferences to control how your script is launched. Option-dragging allows you to change these for one invocation, or use its Preferences menu to change things globally.
+
* 选择 '''PythonLauncher''' 作为默认应用程序以通过 finder 信息窗口打开您的脚本(或任何 .py 脚本)并双击它。 '''PythonLauncher''' 有各种首选项来控制脚本的启动方式。 选项拖动允许您为一次调用更改这些,或使用其首选项菜单全局更改内容。
  
  
第73行: 第48行:
  
 
<span id="osx-gui-scripts"></span>
 
<span id="osx-gui-scripts"></span>
=== <span class="section-number">4.1.2. </span>Running scripts with a GUI ===
+
=== 4.1.2. 使用 GUI 运行脚本 ===
  
With older versions of Python, there is one Mac OS X quirk that you need to be
+
对于旧版本的 Python,您需要注意一个 macOS 怪癖:与 Aqua 窗口管理器对话的程序(换句话说,任何具有 GUI 的程序)需要以特殊方式运行。 使用 '''pythonw''' 而不是 '''python''' 来启动此类脚本。
aware of: programs that talk to the Aqua window manager (in other words,
 
anything that has a GUI) need to be run in a special way. Use '''pythonw'''
 
instead of '''python''' to start such scripts.
 
  
With Python 3.9, you can use either '''python''' or '''pythonw'''.
+
Python 3.9 中,您可以使用 '''python''' '''pythonw'''
  
  
第86行: 第58行:
 
<div id="configuration" class="section">
 
<div id="configuration" class="section">
  
=== <span class="section-number">4.1.3. </span>Configuration ===
+
=== 4.1.3. 配置 ===
  
Python on OS X honors all standard Unix environment variables such as
+
macOS 上的 Python 支持所有标准的 Unix 环境变量,例如 <span id="index-0" class="target"></span>[[../cmdline#envvar-PYTHONPATH|PYTHONPATH]],但是为从 Finder 启动的程序设置这些变量是非标准的,因为 Finder 不会读取您的 [ X219X] 或 <code>.cshrc</code> 在启动时。 您需要创建一个文件 <code>~/.MacOSX/environment.plist</code>。 有关详细信息,请参阅 Apple 的技术文档 QA1067。
<span id="index-0" class="target"></span>[[../cmdline#envvar-PYTHONPATH|<code>PYTHONPATH</code>]], but setting these variables for programs started from the
 
Finder is non-standard as the Finder does not read your <code>.profile</code> or
 
<code>.cshrc</code> at startup. You need to create a file
 
<code>~/.MacOSX/environment.plist</code>. See Apple's Technical Document QA1067 for
 
details.
 
  
For more information on installation Python packages in MacPython, see section
+
有关在 MacPython 中安装 Python 包的更多信息,请参阅 [[#mac-package-manager|安装其他 Python ]] 部分。
[[#mac-package-manager|<span class="std std-ref">Installing Additional Python Packages</span>]].
 
  
  
第105行: 第71行:
  
 
<span id="ide"></span>
 
<span id="ide"></span>
== <span class="section-number">4.2. </span>The IDE ==
+
== 4.2. 集成开发环境 ==
  
MacPython ships with the standard IDLE development environment. A good
+
MacPython 附带了标准的 IDLE 开发环境。 可以在 http://www.hashcollision.org/hkn/python/idle_intro/index.html 找到关于使用 IDLE 的很好的介绍。
introduction to using IDLE can be found at
 
http://www.hashcollision.org/hkn/python/idle_intro/index.html.
 
  
  
第116行: 第80行:
  
 
<span id="mac-package-manager"></span>
 
<span id="mac-package-manager"></span>
== <span class="section-number">4.3. </span>Installing Additional Python Packages ==
+
== 4.3. 安装额外的 Python ==
  
There are several methods to install additional Python packages:
+
有几种方法可以安装额外的 Python 包:
  
* Packages can be installed via the standard Python distutils mode (<code>python setup.py install</code>).
+
* 可以通过标准 Python distutils 模式 (<code>python setup.py install</code>) 安装软件包。
* Many packages can also be installed via the '''setuptools''' extension or '''pip''' wrapper, see https://pip.pypa.io/.
+
* 许多软件包也可以通过 '''setuptools''' 扩展或 '''pip''' 包装器安装,参见 https://pip.pypa.io/
  
  
第127行: 第91行:
 
<div id="gui-programming-on-the-mac" class="section">
 
<div id="gui-programming-on-the-mac" class="section">
  
== <span class="section-number">4.4. </span>GUI Programming on the Mac ==
+
== 4.4. Mac 上的 GUI 编程 ==
  
There are several options for building GUI applications on the Mac with Python.
+
Mac 上使用 Python 构建 GUI 应用程序有多种选择。
  
''PyObjC'' is a Python binding to Apple's Objective-C/Cocoa framework, which is
+
''PyObjC'' 是与 Apple Objective-C/Cocoa 框架的 Python 绑定,它是大多数现代 Mac 开发的基础。 关于 PyObjC 的信息可从 https://pypi.org/project/pyobjc/ 获得。
the foundation of most modern Mac development. Information on PyObjC is
 
available from https://pypi.org/project/pyobjc/.
 
  
The standard Python GUI toolkit is [[../../library/tkinter#module-tkinter|<code>tkinter</code>]], based on the cross-platform
+
标准的 Python GUI 工具包是 [[../../library/tkinter#module-tkinter|tkinter]],基于跨平台 Tk 工具包(https://www.tcl.tk)。 苹果在 OS X 中捆绑了一个 Aqua-native 版本的 Tk,最新版本可以从 https://www.activestate.com 下载和安装; 它也可以从源代码构建。
Tk toolkit (https://www.tcl.tk). An Aqua-native version of Tk is bundled with OS
 
X by Apple, and the latest version can be downloaded and installed from
 
https://www.activestate.com; it can also be built from source.
 
  
''wxPython'' is another popular cross-platform GUI toolkit that runs natively on
+
''wxPython'' 是另一个流行的跨平台 GUI 工具包,它在 macOS 上本地运行。 软件包和文档可从 https://www.wxpython.org 获得。
Mac OS X. Packages and documentation are available from https://www.wxpython.org.
 
  
''PyQt'' is another popular cross-platform GUI toolkit that runs natively on Mac
+
''PyQt'' 是另一个流行的跨平台 GUI 工具包,它在 macOS 上本地运行。 更多信息可以在 https://riverbankcomputing.com/software/pyqt/intro 找到。
OS X. More information can be found at
 
https://riverbankcomputing.com/software/pyqt/intro.
 
  
  
第151行: 第107行:
 
<div id="distributing-python-applications-on-the-mac" class="section">
 
<div id="distributing-python-applications-on-the-mac" class="section">
  
== <span class="section-number">4.5. </span>Distributing Python Applications on the Mac ==
+
== 4.5. 在 Mac 上分发 Python 应用程序 ==
  
The standard tool for deploying standalone Python applications on the Mac is
+
在 Mac 上部署独立 Python 应用程序的标准工具是 '''py2app'''。 有关安装和使用 py2app 的更多信息,请访问 http://undefined.org/python/#py2app。
'''py2app'''. More information on installing and using py2app can be found
 
at http://undefined.org/python/#py2app.
 
  
  
第161行: 第115行:
 
<div id="other-resources" class="section">
 
<div id="other-resources" class="section">
  
== <span class="section-number">4.6. </span>Other Resources ==
+
== 4.6. 其他资源 ==
  
The MacPython mailing list is an excellent support resource for Python users and
+
MacPython 邮件列表是 Mac 上 Python 用户和开发人员的绝佳支持资源:
developers on the Mac:
 
  
 
https://www.python.org/community/sigs/current/pythonmac-sig/
 
https://www.python.org/community/sigs/current/pythonmac-sig/
  
Another useful resource is the MacPython wiki:
+
另一个有用的资源是 MacPython wiki:
  
 
https://wiki.python.org/moin/MacPython
 
https://wiki.python.org/moin/MacPython
第176行: 第129行:
  
 
</div>
 
</div>
 +
<div class="clearer">
  
[[Category:Python 3.9 中文文档]]
+
 
 +
 
 +
</div>
 +
 
 +
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:54的最新版本

4. 在 Mac 上使用 Python

作者
鲍勃·萨维奇< bobsavage@mac.com >

运行 macOS 的 Mac 上的 Python 原则上与任何其他 Unix 平台上的 Python 非常相似,但还有许多附加功能,例如 IDE 和包管理器,值得一提。

4.1. 获取和安装 MacPython

自 10.8 版起的 macOS 随附 Apple 预装的 Python 2.7。 如果您愿意,我们邀请您从 Python 网站 (https://www.python.org) 安装最新版本的 Python 3。 一个当前的 Python 的“通用二进制”版本可以在 Mac 的新 Intel 和传统 PPC CPU 上本地运行,可以在那里获得。

安装后你会得到很多东西:

  • Applications 文件夹中的 Python 3.9 文件夹。 在这里您可以找到 IDLE,它是官方 Python 发行版的标准部分的开发环境; 和 PythonLauncher,它处理来自 Finder 的双击 Python 脚本。
  • 一个框架 /Library/Frameworks/Python.framework,其中包括 Python 可执行文件和库。 安装程序将此位置添加到您的 shell 路径。 要卸载 MacPython,您只需删除这三样东西即可。 Python 可执行文件的符号链接位于 /usr/local/bin/ 中。

Apple 提供的 Python 版本分别安装在 /System/Library/Frameworks/Python.framework/usr/bin/python 中。 您永远不应修改或删除这些内容,因为它们由 Apple 控制并由 Apple 或第三方软件使用。 请记住,如果您选择从 python.org 安装更新的 Python 版本,您的计算机上将有两个不同但功能强大的 Python 安装,因此您的路径和用法与您想要执行的操作一致非常重要。

IDLE 包括一个帮助菜单,允许您访问 Python 文档。 如果您完全不熟悉 Python,您应该开始阅读该文档中的教程介绍。

如果您熟悉其他 Unix 平台上的 Python,您应该阅读有关从 Unix shell 运行 Python 脚本的部分。

4.1.1. 如何运行 Python 脚本

在 macOS 上开始使用 Python 的最佳方法是通过 IDLE 集成开发环境,请参阅部分 IDE 并在 IDE 运行时使用“帮助”菜单。

如果您想从终端窗口命令行或 Finder 运行 Python 脚本,您首先需要一个编辑器来创建您的脚本。 macOS 附带了许多标准的 Unix 命令行编辑器,其中包括 vimemacs。 如果您想要更像 Mac 的编辑器,可以使用 Bare Bones Software 的 BBEditTextWrangler(请参阅 http://www.barebones.com/products/bbedit/index.html)。 html) 是不错的选择,TextMate 也是不错的选择(参见 https://macromates.com/)。 其他编辑器包括 Gvim (http://macvim-dev.github.io/macvim/) 和 Aquamacs (http://aquamacs.组织/)。

要从终端窗口运行您的脚本,您必须确保 /usr/local/bin 在您的 shell 搜索路径中。

要从 Finder 运行脚本,您有两个选择:

  • 将其拖到 PythonLauncher
  • 选择 PythonLauncher 作为默认应用程序以通过 finder 信息窗口打开您的脚本(或任何 .py 脚本)并双击它。 PythonLauncher 有各种首选项来控制脚本的启动方式。 选项拖动允许您为一次调用更改这些,或使用其首选项菜单全局更改内容。


4.1.2. 使用 GUI 运行脚本

对于旧版本的 Python,您需要注意一个 macOS 怪癖:与 Aqua 窗口管理器对话的程序(换句话说,任何具有 GUI 的程序)需要以特殊方式运行。 使用 pythonw 而不是 python 来启动此类脚本。

在 Python 3.9 中,您可以使用 pythonpythonw


4.1.3. 配置

macOS 上的 Python 支持所有标准的 Unix 环境变量,例如 PYTHONPATH,但是为从 Finder 启动的程序设置这些变量是非标准的,因为 Finder 不会读取您的 [ X219X] 或 .cshrc 在启动时。 您需要创建一个文件 ~/.MacOSX/environment.plist。 有关详细信息,请参阅 Apple 的技术文档 QA1067。

有关在 MacPython 中安装 Python 包的更多信息,请参阅 安装其他 Python 包 部分。


4.2. 集成开发环境

MacPython 附带了标准的 IDLE 开发环境。 可以在 http://www.hashcollision.org/hkn/python/idle_intro/index.html 找到关于使用 IDLE 的很好的介绍。


4.3. 安装额外的 Python 包

有几种方法可以安装额外的 Python 包:

  • 可以通过标准 Python distutils 模式 (python setup.py install) 安装软件包。
  • 许多软件包也可以通过 setuptools 扩展或 pip 包装器安装,参见 https://pip.pypa.io/。


4.4. Mac 上的 GUI 编程

在 Mac 上使用 Python 构建 GUI 应用程序有多种选择。

PyObjC 是与 Apple 的 Objective-C/Cocoa 框架的 Python 绑定,它是大多数现代 Mac 开发的基础。 关于 PyObjC 的信息可从 https://pypi.org/project/pyobjc/ 获得。

标准的 Python GUI 工具包是 tkinter,基于跨平台 Tk 工具包(https://www.tcl.tk)。 苹果在 OS X 中捆绑了一个 Aqua-native 版本的 Tk,最新版本可以从 https://www.activestate.com 下载和安装; 它也可以从源代码构建。

wxPython 是另一个流行的跨平台 GUI 工具包,它在 macOS 上本地运行。 软件包和文档可从 https://www.wxpython.org 获得。

PyQt 是另一个流行的跨平台 GUI 工具包,它在 macOS 上本地运行。 更多信息可以在 https://riverbankcomputing.com/software/pyqt/intro 找到。


4.5. 在 Mac 上分发 Python 应用程序

在 Mac 上部署独立 Python 应用程序的标准工具是 py2app。 有关安装和使用 py2app 的更多信息,请访问 http://undefined.org/python/#py2app。


4.6. 其他资源

MacPython 邮件列表是 Mac 上 Python 用户和开发人员的绝佳支持资源:

https://www.python.org/community/sigs/current/pythonmac-sig/

另一个有用的资源是 MacPython wiki:

https://wiki.python.org/moin/MacPython