LoginSignup
1
1

More than 3 years have passed since last update.

pyinstallerによる実行ファイルの作成

Last updated at Posted at 2019-09-06

作成したスクリプトをpyinstallerを使用して実行ファイルにする際に幾つか詰まったところがあったので、記録として残します。

環境は以下のような感じです。
Python-3.7.4
pyinstaller-3.5
PyQt5-5.13.0

pyqtgraphのimport順番

Exception: PyQtGraph requires one of PyQt4, PyQt5 or PySide; none of these packages could be imported.
[5708] Failed to execute script console

というエラーが出ていましたが、原因はimport文を書く順番でした。
スクリプトから実行する場合はある程度順番が逆でも問題ないのですが、pyinstallerで実行ファイルにした際には厳密に処理されるようです。
PyQt5を先にimportして、その後、PyQtGraphのimportをするようにしました。

Qt5Core.dllなどを読み込まない

実行ファイルと同じディレクトリにQt5Core.dllなどを置いても読み込みませんでした。
環境変数でPATHを設定しておけば実行ファイルが起動できるようになることから、PATHが通っていないことがわかりました。

この為、以下のコードをPyQt5のimport文の前に挿入しました。

# Fix qt import error
# Include this file before import PyQt5 

import os
import sys
import logging


def _append_run_path():
    if getattr(sys, 'frozen', False):
        pathlist = []

        # If the application is run as a bundle, the pyInstaller bootloader
        # extends the sys module by a flag frozen=True and sets the app
        # path into variable _MEIPASS'.
        pathlist.append(sys._MEIPASS)

        # the application exe path
        _main_app_path = os.path.dirname(sys.executable)
        pathlist.append(_main_app_path)

        # append to system path enviroment
        os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)

    logging.error("current PATH: %s", os.environ['PATH'])


_append_run_path()

参考にした記事
How to fix “ImportError: unable to find Qt5Core.dll on PATH” after pyinstaller bundled the python application - Stack Overflow

specファイルなどで指定したファイルの読み込み

specファイルのdatasに指定したファイルは実行時にsys._MEIPASSで取得できるディレクトリに展開されるようです。
以下のように設定することで取得が出来ました。

if hasattr(sys, "_MEIPASS"):
    FILE_PATH = os.path.join(sys._MEIPASS, 'test.png')
else:
    FILE_PATH = os.path.join(os.path.dirname(__file__), 'test.png')
1
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
1
1