Macintosh で Python を使う
**************************

Author:
   Bob Savage <bobsavage@mac.com>

Mac OS X が動作している Macintosh 上の Python は原則的には他の Unix プ
ラットフォーム上の Python と非常によく似ていますが、 IDE やパッケージ
・マネージャなどの指摘すべき追加要素があります。

The Mac-specific modules are documented in Mac OS X 固有のサービス.

Python on Mac OS 9 or earlier can be quite different from Python on
Unix or Windows, but is beyond the scope of this manual, as that
platform is no longer supported, starting with Python 2.4. See
http://www.cwi.nl/~jack/macpython for installers for the latest 2.3
release for Mac OS 9 and related documentation.


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

Mac OS X 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
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.

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

* A "MacPython 2.7" folder in your "Applications" folder. In here
  you find IDLE, the development environment that is a standard part
  of official Python distributions; PythonLauncher, which handles
  double- clicking Python scripts from the Finder; and the 「Build
  Applet」 tool, which allows you to package Python scripts as
  standalone applications on your system.

* 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 スクリプトの実行についての節を読むことをおすすめし
ます。


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

Mac OS X で Python を始める最良の方法は統合開発環境である IDLE を使う
ことです、 IDE 節を参照し IDE を実行しているときにヘルプメニューを使っ
てください。

もし Python スクリプトをターミナルのコマンドラインや Finder から実行し
たい場合は最初にエディタでスクリプトを作る必要があります。 Mac OS X に
は **vim** や **emacs** などの Unix の標準のラインエディタが備わってい
ます。もしもっと Mac らしいエディタが欲しい場合には、Bare Bones
Software (http://www.barebones.com/products/bbedit/index.html を参照)
の **BBEdit** や **TextWrangler** もしくは **TextMate**
(https://macromates.com/) は良い選択候補です。他には **Gvim**
(http://macvim.org) や **Aquamacs** (http://aquamacs.org/) などがあり
ます。

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

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

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

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


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

古いバージョンの Python について、気を付けておくべき Mac OS X の癖があ
ります: Aqua ウィンドウマネージャとやりとりをする (別の言い方をすると
GUI を持つ) プログラムは特別な方法で実行する必要があります。そのような
スクリプトを実行するには **python** ではな **pythonw** を使ってくださ
い。

With Python 2.7, you can use either **python** or **pythonw**.


Configuration
-------------

OS X 上の Python では "PYTHONPATH" のような全ての標準の Unix 環境変数
が使えますが、Finder からプログラムを起動する場合このような環境変数を
設定する方法は非標準であり Finder は起動時に ".profile" や ".cshrc" を
読み込みません。 "~/.MacOSX/environment.plist" ファイルを作る必要があ
ります。詳細については Apple の Technical Document QA1067 を参照してく
ださい。

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


IDE
===

MacPython には標準の IDLE 開発環境が付いてきます。
https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html に
IDLE を使うための良い入門があります。


追加の Python パッケージのインストール
======================================

追加の Python パッケージをインストールする方法がいくつかあります:

* パッケージは Python の標準の distutils モードを使ってインストール
  す ることができます ("python setup.py install")。

* 多くのパッケージは **setuptools** 拡張や **pip** ラッパーを使って
  も インストールできます。 https://pip.pypa.io/ を参照してください。


Mac での GUI プログラミング
===========================

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

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

The standard Python GUI toolkit is "Tkinter", based on the cross-
platform 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* は別の人気のあるクロスプラットフォームの GUI ツールキットで
Mac OS X 上でネイティブに動作します。パッケージとドキュメントは
http://www.wxpython.org から利用できます。

*PyQt* は別の人気のあるクロスプラットフォームの GUI ツールキットで Mac
OS X 上でネイティブに動作します。詳しい情報は
https://riverbankcomputing.com/software/pyqt/intro にあります。


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

The 「Build Applet」 tool that is placed in the MacPython 2.7 folder
is fine for packaging small Python scripts on your own machine to run
as a standard Mac application. This tool, however, is not robust
enough to distribute Python applications to other users.

Mac 上の単独の Python アプリケーションをデプロイする標準のツールは
**py2app** です。 py2app のインストールと使用法に関する情報は
http://undefined.org/python/#py2app にあります。


他のリソース
============

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

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

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

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