前提
pyinstallerを使用して作成した実行ファイル(.exe)ファイルが正常に動作しないため、解決法を探しています。
当該プログラムはpywebioを使用したプログラムでwebブラウザ画面を起動させて条件などを入力させる仕様です。
ターミナルから実行(python XXXXX.py)した場合は正常に動作しますが、exe化すると起動に失敗します。
発生している問題・エラーメッセージ
exeファイル起動後にプロンプトに表示されるメッセージの最後尾部分が以下の通りになっています。
File "pywebio\platform\page.py", Tine 28, in <module> FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\(ユーザー名)_\\AppData\\Local\\Temp\\_MEI54162\\pywebio\\platform\\tpl\\index.html' [17868] Failed to execute script '(ファイル名)' due to unhandled exception!
要するに、webioがデフォルトで使用するindex.htmlファイルが見つからないということだと考えています。
実行ファイルとして実行した場合、参照されるフォルダのルートが変わってしまうのが問題だと思いますが、実行時に参照先を変更する方法がわからない状況です。
該当のソースコード
エラーメッセージで参照している"pywebio\platform\page.py"のソース最後の2行でindex.htmlファイルを参照しているのですが、ここが実行ファイル起動時の位置(なぜかユーザーフォルダの一時フォルダ)になってしまい、ファイルが見つからないということです。
python
1import urllib.parse 2from collections import namedtuple 3from collections.abc import Mapping, Sequence 4from functools import lru_cache 5from functools import partial 6from os import path, environ 7 8from tornado import template 9 10from ..__version__ import __version__ as version 11from ..utils import isgeneratorfunction, iscoroutinefunction, get_function_name, get_function_doc, \ 12 get_function_attr, STATIC_PATH 13 14""" 15The maximum size in bytes of a http request body or a websocket message, after which the request or websocket is aborted 16Set by `start_server()` or `path_deploy()` 17Used in `file_upload()` as the `max_size`/`max_total_size` parameter default or to validate the parameter. 18""" 19MAX_PAYLOAD_SIZE = 0 20 21DEFAULT_CDN = "https://cdn.jsdelivr.net/gh/wang0618/PyWebIO-assets@v{version}/" 22 23_global_config = {'title': 'PyWebIO Application'} 24config_keys = ['title', 'description', 'js_file', 'js_code', 'css_style', 'css_file', 'theme'] 25AppMeta = namedtuple('App', config_keys) 26 27_here_dir = path.dirname(path.abspath(__file__)) 28_index_page_tpl = template.Template(open(path.join(_here_dir, 'tpl', 'index.html'), encoding='utf8').read()) 29
実現したい事
webioで使用しているindex.htmlファイルを.exeファイル実行時にも参照できるようにする方法として私としては以下の2通りのどちらかになるかと思っていますが、実現する方法を教えていただきたいです。
方法①
- index.htmlの参照先をexeファイルと同じフォルダを参照するようにpage.pyのソースを書き換える
- exeファイルと同じフォルダにindex.htmlをコピーして置いておく
方法②
pyinstallerでexeファイル作成時にindex.htmlファイルの参照先を指定する
補足情報(FW/ツールのバージョンなど)
実行環境
windows10
VSCode
Python 3.11.1
pywebio 1.7.1
pyinstaller 5.7.0

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/30 13:43
2023/01/30 14:08