5. macOS で Python を使う¶
このドキュメントでは、Mac コンピュータで Python を使い始める際に知っておくべき、macOS 固有の挙動を概説します。 macOS を実行する Mac 上の Python は、ほかの Unix 派生プラットフォーム上の Python と非常によく似ていますが、インストール方法や一部の機能には違いがあります。
macOS 用の Python を入手してインストールする方法はいくつかあります。 最近の Python のバージョンは、複数の配布元からビルド済みのものを入手できます。 このドキュメントの大部分では、CPython リリースチームが python.org のウェブサイト でダウンロード用に提供している Python の使い方を説明します。 ほかの選択肢については Alternative Distributions を参照してください。
5.1. python.org の macOS 用 Python を使う¶
5.1.1. インストール手順¶
For current Python versions
(other than those in security status), the release team produces a
Python for macOS installer package for each new release.
A list of available installers
is available here.
We recommend using the most recent supported Python version where possible.
Current installers provide a
universal2 binary build
of Python which runs natively on all Macs (Apple Silicon and Intel) that are
supported by a wide range of macOS versions,
currently typically from at least macOS 10.13 High Sierra on.
The downloaded file is a standard macOS installer package file (.pkg).
File integrity information (checksum, size, sigstore signature, etc) for each file is included
on the release download page. Installer packages and their contents are signed and notarized
with Python Software Foundation Apple Developer ID certificates
to meet macOS Gatekeeper requirements.
For a default installation, double-click on the downloaded installer package file. This should launch the standard macOS Installer app and display the first of several installer windows steps.
Clicking on the Continue button brings up the Read Me for this installer.
Besides other important information, the Read Me documents which Python version is
going to be installed and on what versions of macOS it is supported. You may need
to scroll through to read the whole file. By default, this Read Me will also be
installed in /Applications/Python 3.13/ and available to read anytime.
Continue をクリックすると、Python および同梱されるほかのソフトウェアのライセンスが表示されます。 次の手順に進むには、ライセンス条項に Agree で同意する必要があります。 このライセンスファイルもインストールされ、後で読むことができます。
ライセンス条項に同意すると、次に Installation Type 画面が表示されます。 ほとんどの用途では、標準のインストール内容が適切です。
By pressing the Customize button, you can choose to omit or select certain package components of the installer. Click on each package name to see a description of what it installs. To also install support for the optional experimental free-threaded feature, see フリースレッドバイナリ (Free-threaded Binaries) のインストール.
どちらの場合も、Install をクリックすると、新しいソフトウェアをインストールするための許可を求めてインストール処理が始まります。
インストールした Python はその Mac のすべてのユーザーが利用できるため、Administrator 権限のある macOS ユーザー名が必要です。
インストールが完了すると、Summary ウィンドウが表示されます。
Double-click on the Install Certificates.command
icon or file in the /Applications/Python 3.13/ window to complete the
installation.
一時的な Terminal シェルウィンドウが開き、新しい Python を使って、その Python 用の SSL ルート証明書をダウンロードしてインストールします。
If Successfully installed certifi and update complete appears
in the terminal window, the installation is complete.
Close this terminal window and the installer window.
デフォルトのインストールには、次のものが含まれます:
A
Python 3.13folder in yourApplicationsfolder. In here you find IDLE, the development environment that is a standard part of official Python distributions; and Python Launcher, which handles double-clicking Python scripts from the macOS Finder.A framework
/Library/Frameworks/Python.framework, which includes the Python executable and libraries. The installer adds this location to your shell path. To uninstall Python, you can remove these three things. Symlinks to the Python executable are placed in/usr/local/bin/.
注釈
最近の macOS には、/usr/bin/python3 に python3 コマンドが含まれています。
これは、Apple の開発ツールである Xcode または Command Line Tools for Xcode によって、それらのツールで使用するために提供される Python にリンクしており、通常は古く、不完全なバージョンです。
この Python は Apple が管理し、Apple 製やサードパーティのソフトウェアが使用するため、決して変更したり、削除しようとしたりしないでください。
python.org から新しいバージョンの Python をインストールすると、コンピュータには異なる 2 つの Python 環境が存在することになりますが、両方とも動作し、共存できます。
インストーラのデフォルトのオプションでは、システムの python3 に代わって、インストールした python3 が使用されるはずです。
5.1.2. Python スクリプトの実行方法¶
There are two ways to invoke the Python interpreter.
If you are familiar with using a Unix shell in a terminal
window, you can invoke python3.13 or python3 optionally
followed by one or more command line options (described in コマンドラインと環境).
The Python tutorial also has a useful section on
using Python interactively from a shell.
統合開発環境からインタープリタを起動することもできます。 IDLE --- Python のエディタとシェル は、Python の標準配布物に含まれる基本的なエディタとインタープリタの環境です。 IDLE の Help メニューから Python のドキュメントを参照できます。 Python を初めて使う場合は、そのドキュメントにあるチュートリアルの導入部分を読むとよいでしょう。
ほかにも多くのエディタや IDE を利用できます。 詳細は エディタと IDE を参照してください。
ターミナルウィンドウから Python スクリプトファイルを実行するには、スクリプトファイル名を指定してインタープリタを起動します:
python3.13myscript.py
Finder からスクリプトを実行するには、次のいずれかの方法を使います:
スクリプトを Python Launcher にドラッグします。
Finder の情報ウィンドウで、スクリプト (または任意の
.pyスクリプト) を開くデフォルトのアプリケーションとして Python Launcher を選択し、スクリプトをダブルクリックします。 Python Launcher には、スクリプトの起動方法を制御するさまざまな環境設定があります。 Option キーを押しながらドラッグすると、今回の起動に限って設定を変更できます。 全体の設定を変更するには、Preferencesメニューを使います。
macOS の Finder から直接スクリプトを実行すると、ターミナルウィンドウから実行した場合と結果が異なることがあります。 シェルのプロファイルでの環境変数の設定など、通常のシェル環境でスクリプトが実行されるわけではないためです。 また、ほかのスクリプトやプログラムを実行するときと同様に、何を実行しようとしているかを確認してください。
5.2. Alternative Distributions¶
macOS 用の標準の python.org インストーラに加えて、追加の機能を含む場合があるサードパーティの macOS 用配布物もあります。
よく使われる配布物とその主な特徴を紹介します:
- ActivePython
複数のプラットフォームに対応するインストーラ、ドキュメント
- Anaconda
よく使われる科学計算モジュール (numpy、scipy、pandas など) と
condaパッケージマネージャ。- Homebrew
複数バージョンの Python と、Python に基づく多くのサードパーティパッケージ (numpy、scipy、pandas など) を含む macOS 用パッケージマネージャ。
- MacPorts
複数バージョンの Python と、Python に基づく多くのサードパーティパッケージを含む、もう一つの macOS 用パッケージマネージャ。 古い macOS バージョン用にビルドされた Python や多くのパッケージを含む場合があります。
これらの配布物には最新バージョンの Python やほかのライブラリが含まれていないことがあり、Python のコアチームによる保守やサポートも行われていない点に注意してください。
5.3. 追加の Python パッケージのインストール¶
詳細は Python Packaging User Guide を参照してください。
5.4. GUI プログラミング¶
Python で Mac 上の GUI アプリケーションをビルドする方法がいくつかあります。
Python の標準 GUI ツールキットは、複数のプラットフォームに対応する Tk ツールキット (https://www.tcl.tk) を基にした tkinter です。
インストーラには、macOS でネイティブに動作するバージョンの Tk が含まれています。
PyObjC は、Apple の Objective-C/Cocoa フレームワークの Python バインディングです。 PyObjC についての情報は pyobjc を参照してください。
macOS 用の GUI ツールキットには、次のようなほかの選択肢もあります:
PySide: Qt GUI ツールキット の公式 Python バインディング。
PyQt: Qt 用の別の Python バインディング。
Kivy: デスクトップとモバイルのプラットフォームに対応する GUI ツールキット。
Toga: BeeWare Project の一部で、デスクトップ、モバイル、ウェブ、コンソールのアプリに対応します。
wxPython: 複数のデスクトップ OS に対応するツールキット。
5.5. 高度なトピック¶
5.5.1. フリースレッドバイナリ (Free-threaded Binaries) のインストール¶
Added in version 3.13: (実験的)
注釈
このセクションで説明されているすべての項目は実験的とみなされており、将来のリリースで変更される可能性があります。
The python.org Python for macOS
installer package can optionally install an additional build of
Python 3.13 that supports PEP 703, the experimental free-threading feature
(running with the global interpreter lock disabled).
Check the release page on python.org for possible updated information.
Because this feature is still considered experimental, the support for it is not installed by default. It is packaged as a separate install option, available by clicking the Customize button on the Installation Type step of the installer as described above.
If the box next to the Free-threaded Python package name is checked,
a separate PythonT.framework will also be installed
alongside the normal Python.framework in /Library/Frameworks.
This configuration allows a free-threaded Python 3.13 build to co-exist
on your system with a traditional (GIL only) Python 3.13 build with
minimal risk while installing or testing. This installation layout is itself
experimental and is subject to change in future releases.
既知の注意点と制限:
The UNIX command-line tools package, which is selected by default, will install links in
/usr/local/binforpython3.13t, the free-threaded interpreter, andpython3.13t-config, a configuration utility which may be useful for package builders. Since/usr/local/binis typically included in your shellPATH, in most cases no changes to yourPATHenvironment variables should be needed to usepython3.13t.For this release, the Shell profile updater package and the
Update Shell Profile.commandin/Applications/Python 3.13/do not support the free-threaded package.The free-threaded build and the traditional build have separate search paths and separate
site-packagesdirectories so, by default, if you need a package available in both builds, it may need to be installed in both. The free-threaded package will install a separate instance of pip for use withpython3.13t.venv を使わずに pip でパッケージをインストールするには、次のようにします:
python3.13t -m pip install <package_name>
複数の Python 環境を使用する場合は、通常、仮想環境を作成して使用する のが最も安全で簡単です。 これにより、コマンド名が衝突したり、どの Python を使用しているか分からなくなったりするのを避けられます:
python3.13t -m venv <venv_name>
続いて activate を実行します。
フリースレッド版の IDLE を実行するには、次のようにします:
python3.13t -m idlelib
どちらのビルドのインタープリタも同じ PYTHON 環境変数 を参照するため、例えばシェルのプロファイルで
PYTHONPATHを設定していると、予期しない結果になる場合があります。 必要に応じて、-Eのような コマンドラインオプション を使い、これらの環境変数を無視できます。フリースレッドビルドは、従来のフレームワークにインストールされた
OpenSSLやTkなどのサードパーティの共有ライブラリにリンクします。 そのため、両方のビルドは Install Certificates.command スクリプトでインストールした信頼証明書も共有するので、このスクリプトは一度実行するだけで済みます。If you cannot depend on the link in
/usr/local/binpointing to thepython.orgfree-threadedpython3.13t(for example, if you want to install your own version there or some other distribution does), you can explicitly set your shellPATHenvironment variable to include thePythonTframeworkbindirectory:export PATH="/Library/Frameworks/PythonT.framework/Versions/3.13/bin":"$PATH"
The traditional framework installation by default does something similar, except for
Python.framework. Be aware that having both frameworkbindirectories inPATHcan lead to confusion if there are duplicate names likepython3.13in both; which one is actually used depends on the order they appear inPATH. Thewhich python3.xorwhich python3.xtcommands can show which path is being used. Using virtual environments can help avoid such ambiguities. Another option might be to create a shell alias to the desired interpreter, like:alias py3.13="/Library/Frameworks/Python.framework/Versions/3.13/bin/python3.13" alias py3.13t="/Library/Frameworks/PythonT.framework/Versions/3.13/bin/python3.13t"
5.5.2. コマンドラインからのインストール¶
If you want to use automation to install the python.org installer package
(rather than by using the familiar macOS Installer GUI app),
the macOS command line installer utility lets you select non-default
options, too. If you are not familiar with installer, it can be
somewhat cryptic (see man installer for more information).
As an example, the following shell snippet shows one way to do it,
using the 3.13.0b2 release and selecting the free-threaded interpreter
option:
RELEASE="python-3.13.0b2-macos11.pkg"
# download installer pkg
curl -O https://www.python.org/ftp/python/3.13.0/${RELEASE}
# create installer choicechanges to customize the install:
# enable the PythonTFramework-3.13 package
# while accepting the other defaults (install all other packages)
cat > ./choicechanges.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>attributeSetting</key>
<integer>1</integer>
<key>choiceAttribute</key>
<string>selected</string>
<key>choiceIdentifier</key>
<string>org.python.Python.PythonTFramework-3.13</string>
</dict>
</array>
</plist>
EOF
sudo installer -pkg ./${RELEASE} -applyChoiceChangesXML ./choicechanges.plist -target /
You can then test that both installer builds are now available with something like:
$ # test that the free-threaded interpreter was installed if the Unix Command Tools package was enabled $ /usr/local/bin/python3.13t -VV Python 3.13.0b2 experimental free-threading build (v3.13.0b2:3a83b172af, Jun 5 2024, 12:57:31) [Clang 15.0.0 (clang-1500.3.9.4)] $ # and the traditional interpreter $ /usr/local/bin/python3.13 -VV Python 3.13.0b2 (v3.13.0b2:3a83b172af, Jun 5 2024, 12:50:24) [Clang 15.0.0 (clang-1500.3.9.4)] $ # test that they are also available without the prefix if /usr/local/bin is on $PATH $ python3.13t -VV Python 3.13.0b2 experimental free-threading build (v3.13.0b2:3a83b172af, Jun 5 2024, 12:57:31) [Clang 15.0.0 (clang-1500.3.9.4)] $ python3.13 -VV Python 3.13.0b2 (v3.13.0b2:3a83b172af, Jun 5 2024, 12:50:24) [Clang 15.0.0 (clang-1500.3.9.4)]
注釈
現在の python.org インストーラは、/Library/Frameworks/、/Applications、/usr/local/bin などの決められた場所にのみインストールします。
installer の -domain オプションを使って、ほかの場所にインストールすることはできません。
5.5.3. Python アプリケーションの配布¶
Python コードをスタンドアローンな配布アプリケーションに変換するツールには様々なものがあります:
py2app: Python プロジェクトから macOS の
.appバンドルを作成できます。Briefcase: BeeWare Project の一部で、複数のプラットフォームに対応するパッケージングツールです。 macOS 上での
.appバンドルの作成や、署名と公証の管理に対応します。PyInstaller: 複数のプラットフォームに対応するパッケージングツールで、配布物として単一のファイルまたはフォルダを作成します。
5.5.4. App Store コンプライアンス¶
macOS App Store を通じて配布するために提出するアプリは、Apple のアプリ審査に合格する必要があります。 この審査には、提出されたアプリケーションバンドル内に問題のあるコードがないかを調べる、一連の自動検証ルールが含まれています。
Python 標準ライブラリには、これらの自動ルールに違反することが分かっているコードが含まれています。 これらの違反は誤検出と思われますが、Apple の審査ルールに異議を唱えることはできません。 そのため、アプリが App Store の審査に合格するには、Python 標準ライブラリを変更する必要があります。
Python ソースツリーには、App Store の審査で問題を引き起こすことが分かっているコードをすべて削除する パッチファイル が含まれています。
このパッチは、CPython の configure に --with-app-store-compliance オプションを指定した場合に自動で適用されます。
Mac 上で CPython を使用するために、通常このパッチは必要ありません。 macOS App Store の 外 でアプリを配布する場合にも必要ありません。 このパッチが必要になるのは、macOS App Store を配布経路として使用する場合 だけ です。
5.6. 他のリソース¶
python.org のヘルプページ には、多くの役立つリソースへのリンクがあります。 Pythonmac-SIG メーリングリスト も、Mac 上で Python を使うユーザーや開発者に特化したサポートリソースです。