プロジェクト

全般

プロフィール

Python開発環境 macOS

macOSのPython実行環境

macOSのバージョンとPython

macOS 12 Monterey

標準では Python 実行環境が用意されなくなりました。
コマンドライン開発ツールを追加インストールすることで Python が利用可能となります。

OSバージョン OS標準 Ptyhon Version Path
macOS 12.5.1 No, 別途コマンドライン開発者ツールに含まれる 3.8.9 /usr/bin/python3

HomeBrewでPythonをインストールすることが可能です。
HomeBrewでは、python3.7〜3.11(2023-04-22時点)が提供されています。

/opt/homebrew/bin/python3

HomeBrewでPython

HomeBrewでPythonが提供されています。

brew でpythonを探す

pythonをbrew searchで検索します。python 3.7〜3.10が提供されています。

% brew search python
==> Formulae
app-engine-python          python-build               python@3.7
boost-python3              python-launcher            python@3.8
bpython                    python-markdown            python@3.9
gst-python                 python-tabulate            reorder-python-imports
ipython                    python-tk@3.10             wxpython
libpython-tabulate         python-tk@3.9              pythran
micropython                python-yq                  jython
ptpython                   python@3.10 ✔              cython

==> Casks
awips-python                             mysql-connector-python

If you meant "python" specifically:
It was migrated from homebrew/cask to homebrew/core.

インストールは、brew install pythonで最新版のpython@3.10がインストールできます。(上述のsearchは既にpython 3.10がインストールされた後の実施例)

tkinterを使う

HomeBrewでインストールするPython3には、tkinterが含まれていません。HomeBrewではpython3とは別にpython-tkが提供されているので、併せてインストールします。

% brew install python-tk

仮想環境

venv

PythonをOS共通の領域にインストールし、そこに色々なアプリケーションでそれぞれ個別に必要とするパッケージを共通領域にインストールすると色々な干渉が生じます。
そこで、Pythonの標準機能 venv を使って、アプリケーション毎に固有のpython実行環境を「仮想環境」として用意します。

使用例

venv作成

helloディレクトリを作成し、そこに Hello worldプログラムとvenv仮想実行環境を用意します。

work% mkdir hello
work% cd hello
hello% python3 -m venv venv
hello% % ls -F venv
bin/        include/    lib/        pyvenv.cfg
  • -m venv でvenvモジュールを実行、最後の引数のvenvは作成するディレクトリ名です。(たまたま同じvenvとしているだけです)
仮想環境の実行
hello% source venv/bin/activate
(venv) hello % python --version
Python 3.10.6
仮想環境に、外部ライブラリのインストール
(venv) hello % pip install matplotlib

仮想環境にインストールした外部ライブラリの一覧を表示

(venv) hello % pip list
Package         Version
--------------- -------
cycler          0.11.0
fonttools       4.37.1
kiwisolver      1.4.4
matplotlib      3.5.3
numpy           1.23.3
packaging       21.3
Pillow          9.2.0
pip             22.2.2
pyparsing       3.0.9
python-dateutil 2.8.2
setuptools      63.4.3
six             1.16.0
(venv) hello % 

venvディレクトリの下を覗いてみると、外部ライブラリが保管されているのがわかります。

(venv) hello % ls venv/lib/python3.10/site-packages 
PIL                    numpy
Pillow-9.2.0.dist-info            numpy-1.23.3.dist-info
__pycache__                packaging
_distutils_hack                packaging-21.3.dist-info
cycler-0.11.0.dist-info            pip
cycler.py                pip-22.2.2.dist-info
dateutil                pkg_resources
distutils-precedence.pth        pylab.py
fontTools                pyparsing
fonttools-4.37.1.dist-info        pyparsing-3.0.9.dist-info
kiwisolver                python_dateutil-2.8.2.dist-info
kiwisolver-1.4.4.dist-info        setuptools
matplotlib                setuptools-63.4.3.dist-info
matplotlib-3.5.3-py3.10-nspkg.pth    six-1.16.0.dist-info
matplotlib-3.5.3.dist-info        six.py
mpl_toolkits
プログラムの記述

matlibplotのサンプルコードを記述します。
https://matplotlib.org/stable/users/getting_started/

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 200)  # 0以上 2π以下の範囲で等間隔の200個の値を numpyのndarray として生成
y = np.sin(x)

fig, ax = plt.subplots()  # Figure(描画領域)と Axes(グラフ)を同時に生成、ここはAxesが1個
ax.plot(x, y)
plt.show()
実行
(venv) hello % python hello_plot.py

venv でmatplotlibのサンプル

仮想環境の利用終了

deactivateを実行すると、仮想環境を終了します。

(venv) hello % deactivate
hello % 


7ヶ月前に更新