4. Using Python on a Mac
************************

著者:
   Bob Savage <bobsavage@mac.com>

Python on a Mac running macOS is in principle very similar to Python
on 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.


4.1. MacPython の入手とインストール
===================================

macOS since version 10.8 comes with Python 2.7 pre-installed by Apple.
If you wish, you are invited to install the most recent version of
Python 3 from the Python website (https://www.python.org).  A current
"universal binary" build of Python, which runs natively on the Mac's
new Intel and legacy PPC CPU's, is available there.

インストールを行うといくつかのものが手に入ります:

* "Applications" フォルダにある "Python 3.9" フォルダ。公式の Python
  ディストリビューションに含まれる開発環境 IDLE; と Finder から Python
  スクリプトをダブルクリックしたときに起動する PythonLauncher。

* Python 実行ファイルやライブラリを含む
  "/Library/Frameworks/Python.framework" フレームワーク。インストーラ
  はシェルのパスにこの場所を追加します。 MacPython をアンインストール
  するには、これら 3 つを削除すればよいだけです。 Python 実行ファイル
  へのシンボリックリンクは /usr/local/bin/ に置かれています。

Apple が提供している Python のビルドは
"/System/Library/Frameworks/Python.framework" と "/usr/bin/python" に
それぞれインストールされています。これらは Apple が管理しているもので
あり Apple やサードパーティのソフトウェアが使用するので、編集したり削
除したりしてはいけません。 python.org から新しいバージョンの Python を
インストールする場合には、それぞれ正常に動作する 2 つの異なる Python
環境がコンピュータにインストールされるため、意図する方の Python のパス
を設定し、使用することが重要です。

IDLE にはヘルプメニューがあり Python のドキュメントにアクセスすること
ができます。もし Python が全くの初めての場合にはドキュメントのチュート
リアルを最初から読み進めることをおすすめします。

もし他の Unix プラットフォームで Python を使い慣れている場合には Unix
シェルからの Python スクリプトの実行についての節を読むことをおすすめし
ます。


4.1.1. Python スクリプトの実行方法
----------------------------------

Your best way to get started with Python on macOS is through the IDLE
integrated development environment, see section IDE 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 the Finder you first need an editor to create your
script. macOS 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/).

ターミナルからスクリプトを実行するには "/usr/local/bin" がシェルのパス
に含まれていることを確認してください。

Finder からスクリプトを実行するのには 2 つの方法があります:

* **PythonLauncher** へドラッグする

* Finder の情報ウィンドウから **PythonLauncher** をそのスクリプト (も
  しくは .py スクリプト全て) を開くデフォルトのアプリケーションとして
  選び、スクリプトファイルをダブルクリックしてください。
  **PythonLauncher** の環境設定にはどのようにスクリプトを実行するかを
  管理する様々な設定があります。 option キーを押しながらドラッグするこ
  とで実行するごとにこれらの設定を変えられますし、環境設定メニューから
  全ての実行に対して設定変更することもできます。


4.1.2. GUI でスクリプトを実行
-----------------------------

With older versions of Python, there is one macOS quirk that you need
to be 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.

Python 3.9 では、 **python** と **pythonw** のどちらでも使えます。


4.1.3. Configuration
--------------------

Python on macOS honors all standard Unix environment variables such as
"PYTHONPATH", but setting these variables for programs started from
the Finder is non-standard as the Finder does not read your ".profile"
or ".cshrc" at startup. You need to create a file
"~/.MacOSX/environment.plist". See Apple's Technical Document QA1067
for details.

MacPython の Python パッケージのインストールについてのさらなる情報は、
追加の Python パッケージのインストール 節を参照してください。


4.2. IDE
========

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 プログラミング
================================

Python で Mac 上の GUI アプリケーションをビルドする方法がいくつかあり
ます。

*PyObjC* は Mac の最新の開発基盤である Apple の Objective-C/Cocoa フレ
ームワークへの Python バインディングです。PyObjC の情報は
https://pypi.org/project/pyobjc/ にあります。

Python 標準の GUI ツールキットは、クロスプラットフォームの Tk ツールキ
ット (https://www.tcl.tk) 上に構築された "tkinter" です。 Tk の Aqua
ネイティブバージョンは Apple が OS X にバンドルしており、最新バージョ
ンは https://www.activestate.com からダウンロードおよびインストールで
きます; またソースからビルドすることもできます。

*wxPython* is another popular cross-platform GUI toolkit that runs
natively on macOS. Packages and documentation are available from
https://www.wxpython.org.

*PyQt* is another popular cross-platform GUI toolkit that runs
natively on macOS. More information can be found at
https://riverbankcomputing.com/software/pyqt/intro.


4.5. Mac 上の Python アプリケーションの配布
===========================================

The standard tool for deploying standalone Python applications on the
Mac is **py2app**. More information on installing and using py2app can
be found at https://pypi.org/project/py2app/.


4.6. 他のリソース
=================

MacPython メーリングリストは Mac での Python ユーザや開発者にとって素
晴しいサポートリソースです:

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

他の役に立つリソースは MacPython wiki です:

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