LoginSignup
10
18

More than 3 years have passed since last update.

PyInstallerで作成した実行可能ファイル(.exe)が動かないときの調査方法

Last updated at Posted at 2019-10-31

事象

PyInstallerでexeを作って実行したが動かない。
VSCodeからは動く。
exe化するとローカルでも動かない。

調査方法

--noconsole"を外して、--debug allをつける。
--noconsoleをつけたままだと、大量のポップアップで泣く。
--debug allをつけることで、コマンドプロンプトに大量のログが出力されるようになる。

pyinstaller myprogram.py --onefile --debug all
[12808] PyInstaller Bootloader 3.x
[12808] LOADER: executable is C:\******
[12808] LOADER: homepath is C:\******
[12808] LOADER: _MEIPASS2 is NULL
...
[6072] PyInstaller Bootloader 3.x
[6072] LOADER: executable is C:\******
...
[6072] LOADER: Runtime option: v
[6072] LOADER: Initializing python
import _frozen_importlib # frozen
import _imp # builtin
import '_thread' # <class '_frozen_importlib.BuiltinImporter'>
import '_warnings' # <class '_frozen_importlib.BuiltinImporter'>
import '_weakref' # <class '_frozen_importlib.BuiltinImporter'>
# installing zipimport hook
import 'zipimport' # <class '_frozen_importlib.BuiltinImporter'>
...

こんな感じで1000行近く出力される。

テキストに貼り付けて、errorを検索する。

import 'myprogram_config' # <pyimod03_importers.FrozenImporter object at 0x03E5C3B0>
Traceback (most recent call last):
  File "myprogram.py", line 16, in <module>
OSError: [WinError 123] ファイル名、ディレクトリ名、またはボリューム ラベルの構文が間違っています。: ''
[6072] Failed to execute script myprogram
[6072] LOADER: OK.
[6072] LOADER: Cleaning up Python interpreter.

おそらくこれが原因。

今回の問題

改修前
# ルートディレクトリ変更
os.chdir(os.path.dirname(__file__))

python 指定 os.path.dirname(file) は空を返します。

改修後
# ルートディレクトリ変更
os.chdir(os.path.dirname(os.path.abspath(__file__)))

プログラムがおいてあるパスを取得したかっただけなんだけど、
前者はexeにしたときはだめらしい。

10
18
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
10
18