at backyard

Color my life with the chaos of trouble.

Mac環境でpyinstallerを使ってOSError: Python library not found: libpython3.9m.dylib, Python, Python3, libpython3.9.dylib,というエラーが出た場合の対処方法

Mac環境で作成しているPythonプログラムを実行ファイルにまとめようとした際に発生したエラーの備忘録です。

実行ファイルにはpyinstallerを用いています。

github.com

なお環境としてはM1チップのMacBook Airを使用しており、Pythonはpyenvを通じてインストール、バージョンは 3.9.6 です。

pyinstallerを使って実行ファイルを作成しようとしたら、下記のようなエラーが出ました。

OSError: Python library not found: libpython3.9m.dylib, Python, Python3, libpython3.9.dylib, .Python
    This means your Python installation does not come with proper shared library files.
    This usually happens due to missing development package, or unsuitable build parameters of the Python installation.

    * On Debian/Ubuntu, you need to install Python development packages:
      * apt-get install python3-dev
      * apt-get install python-dev
    * If you are building Python by yourself, rebuild with `--enable-shared` (or, `--enable-framework` on macOS).

このエラー文に従い、下記のようにオプションを付けて再インストールしました。

PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.9.6

次にpyinstallerを使ってみると、ないと言われます。

pyinstaller sample.py
# => command not found: pyinstaller

そこで試しにpip installしてみると、

pip install pyinstaller
Requirement already satisfied: pyinstaller in ....
...
...
...

という形ですでにインストールされていると言われます。

おそらくpathが通っていないだけな気もしますが一度uninstallしてから再度installしたら直ったと書かれている方がいたので、そちらのやり方にしがってみることにします。
(パスだけの問題ではない可能性ももちろんあるので)

以下実行ログ

pip uninstall pyinstaller
# 下記のようなログが出る
# Found existing installation: pyinstaller 4.7
# Uninstalling pyinstaller-4.7:
...
...
...

再度インストール

pip install pyinstaller

これで再度試してみると無事に処理が完了できました。

pyinstaller sample.py