2
1

More than 1 year has passed since last update.

【Python3.8(32-bit)】おそらく「matplotlib」と「Pyinstaller」の互換性で悩んだが「cx_Freeze」を使ったらあっさり解決【Pythonプログラムのexe化】

Last updated at Posted at 2022-02-11

はじめに

これはPython3.8で「matplotlib」モジュールを使ったpyスクリプトをexe化しようと思ったときに、
「Pyinstaller」で出来ず数ヵ月悩んでいましたが、
改めて「cx_Freeze」を使ったらあっさり解決できた話です。

さきに結論

(ジコログさん)cx_Freezeの使い方【Pythonプログラムのexe化】
cxfreeze -c [pythonスクリプト名].py --target-dir target
・・・これだけで解決しました。。
※「cx_Freeze」には「Pyinstaller」のようにonefileオプションは無いようですが、とりあえずOKとしました。(何よりexeが動いてくれないとならないので。)

ジコログさんが書かれている通り「cx_Freeze」は以下の事さえ気を付けておけば上手く動いてくれるものなのかもしれません。

あとは、Pythonのバージョンに関しては要注意です。

今現在の最新バージョン6.5.3を利用するなら、Pythonのバージョンは3.6~3.9までになります。
また、開発環境のPythonバージョンがPython 3.5.2なら、最新のcx_Freezeは使えないということです。

つまり、開発環境のPythonバージョンに適したcx_Freezeを選択してインストールしないといけません。
このことにさえ注意すれば、それ以外が特に何も意識はする必要はありませんね。

苦闘していた環境

Python利用環境
・Python3.8(32-bit)
・Windows10 Pro(64bit)

exe化しようとしていたpyスクリプト(import部分だけ抜粋)

[pythonスクリプト名].py
import os
import datetime

from xml.dom import minidom #xmlのdom操作

import matplotlib as mpl    #グラフ操作(pip install matplotlib)
import matplotlib.pyplot as plt
mpl.use('Agg') 

#以下略

⚠ 実際のコードそのまま書いていますが本来、matplotlib.use('Agg')の位置はpyplotが呼ばれる前でないといけないそうです。1

2020.12頃(Pyinstaller苦闘の開始~放置)

まずは普通に「PyInstaller」でexe化を試みました。
(Qiita)PyInstallerでexeファイル化
pyinstaller [pythonスクリプト名].py --onefile

するとここに記載されているようなエラーが出ました。。
(Qiita)PythonプログラムをExeファイル化出来なくて1日費やしたときには

RecursionError: maximum recursion depth exceeded

⇒既にこの記事内で、pyinstallerの設定ファイルをいじって解決させるのような手段を記載いただいていますが、ちょっと時間的に着手できず、試すことなく放置してしまいました。。

(参考)Google検索「python exe matplotlib」結果

2021.8頃(Pyinstaller再挑戦でちょっと兆し~cx_Freezeに変えて解決)

この間、WindowsアップデートやPyinstallerアップデートを行い、再び試してみると、
exe生成まではできるようになっていました!(ちょっと進歩。)

しかしそのexeを実行するとエラーが出るのです。。

>[pythonスクリプト名].exe
c:\program files (x86)\python38-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py:493: MatplotlibDeprecationWarning: Matplotlib installs where the data is not in the mpl-data subdirectory of the package are deprecated since 3.2 and support for them will be removed two minor releases later.
  exec(bytecode, module.__dict__)
Traceback (most recent call last):
  File "[pythonスクリプト名].py", line 6, in <module>
    import matplotlib as mpl    #グラフ操作(pip install matplotlib)
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\program files (x86)\python38-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "matplotlib\__init__.py", line 898, in <module>
  File "matplotlib\cbook\__init__.py", line 480, in _get_data_path
  File "matplotlib\__init__.py", line 239, in wrapper
  File "matplotlib\__init__.py", line 534, in get_data_path
  File "matplotlib\__init__.py", line 239, in wrapper
  File "matplotlib\__init__.py", line 566, in _get_data_path
RuntimeError: Could not find the matplotlib data files
[3920] Failed to execute script [pythonスクリプト名]

exe化するとmatplotlibのパスが見えなくなるようなのです。。

RuntimeError: Could not find the matplotlib data files

そして__init__.pyなどでGoogle検索して次のブログを見つけました。
※なおこの時点で私は「cx_Freeze」を知りませんでした。
(診療放射線技師がPythonをはじめました。さん)cx_Freezeでexe化。「matplotlibのモジュールが見つかりません」のエラー対処
ではこの空の__init__.pyファイルを作る方法をPyinstallerで応用しようとやってみたのですが状況が改善できず。。

ふと、そういえば「cx_Freeze」と言う別のexe化するツールもあるのか?とやっと「cx_Freeze」の存在を知り、
やってみるとあっさり解決できた、ということでした。

その他

(Qiita)cx_freeze or PyInstaller ?


  1. (みんなのふぃじー(はてなブログ版)さん)matplotlib.use('Agg')するときの注意点 

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1