Closed tangtao007 closed 1 year ago
抱歉,我也没有看出有什么问题。我这边用的python3.9和Win11测试,没有问题。或者,你用python3.9再试试,可以吗?
应该是vite的问题,我也打包后打开exe空白,具体找不到是vite哪里问题
我遇到同样的问题,已经解决。
开发环境pywebview可以正常显示,生产环境下(如build后生成的exe)白屏。
在main.py中创建窗口(webview.create_window
)前加入以下代码:
# 修复,解决无法加载js文件的问题
import mimetypes
mimetypes.add_type('application/javascript', '.js')
长话短说:pywebview无法加载vite生成的网页资源中的javascript模块。
build使用的是生产环境,不同于开发环境,pywebview加载的是vite生成的本地文件index.html。打开index.html,可以看到javascript是以module类型引入的:
而pywebview内部的浏览器无法正确将js文件识别为module类型,因此无法加载。作为佐证,可以将main.py中的Config.devEnv强制设为False,template设为gui中打包好的index.html,pywebview的debug打开:
然后使用pnpm -C ./gui run build
命令让vite打包,再运行修改好的main.py(python main.py
),从而让pywebview加载生产环境下vite打包生成的网页,在debug窗口就可以发现问题症结:
报错已经将问题说得很清楚了:
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.
解决思路
参考issue的讨论,让pywebview能够正确识别js的module类型即可。根本上还是因为pywebview不能充分模拟浏览器的全部功能。
另外,有趣的是,如果不是用vite构建的vue项目并打包,而是用之前的Vue CLI构建项目并打包,就不会有这些问题。这是因为Vue CLI生成的index.html中js是用text/javascript
的类型引入的,而不是module
类型:
没有修改任何配置文件
pnpm run build:folder
vite v4.5.0 building for production... ✓ 1391 modules transformed. dist/index.html 0.56 kB │ gzip: 0.33 kB dist/assets/vue-03d6d6da.png 6.85 kB dist/assets/pywebview-94b84743.png 12.18 kB dist/assets/pyinstaller-77ebb35d.png 69.30 kB dist/assets/index-e93db3f9.css 39.37 kB │ gzip: 10.44 kB dist/assets/.pnpm-837245ef.css 326.54 kB │ gzip: 44.71 kB dist/assets/index-1124084b.js 7.30 kB │ gzip: 3.33 kB dist/js/.pnpm/.pnpm.3dd875e8.js 1,031.24 kB │ gzip: 326.24 kB ✓ built in 13.29s 398 INFO: PyInstaller: 5.10.1 398 INFO: Python: 3.7.0 398 INFO: Platform: Windows-10-10.0.19041-SP0 398 INFO: UPX is not available. 398 INFO: Removing temporary files and cleaning cache in C:\Users\zxai\AppData\Local\pyinstaller 413 DEPRECATION: Bytecode encryption will be removed in PyInstaller v6. Please remove cipher and block_cipher parameters from your spec file to avoid breakages on upgrade. For the rationale/alternatives see https://github.com/py installer/pyinstaller/pull/6999 413 INFO: Extending PYTHONPATH with paths ['C:\Users\zxai\Desktop\cef\PPX-main'] 726 INFO: Will encrypt Python bytecode with provided cipher key 726 INFO: Appending 'datas' from .spec 726 INFO: checking Analysis 726 INFO: Building Analysis because Analysis-00.toc is non existent 741 INFO: Initializing module dependency graph... 741 INFO: Caching module graph hooks... 757 INFO: Analyzing base_library.zip ... 2804 INFO: Loading module hook 'hook-heapq.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 2866 INFO: Loading module hook 'hook-encodings.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 3695 INFO: Loading module hook 'hook-pickle.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 5038 INFO: Caching module dependency graph... 5195 INFO: running Analysis Analysis-00.toc 5210 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\scripts\python.exe 5257 INFO: Analyzing C:\Users\zxai\Desktop\cef\PPX-main\main.py 5273 INFO: Loading module hook 'hook-webview.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'... 5820 INFO: Loading module hook 'hook-platform.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 5929 INFO: Loading module hook 'hook-importlib_metadata.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 6130 INFO: Loading module hook 'hook-mako.codegen.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'... 6382 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 6601 INFO: Loading module hook 'hook-xml.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 6773 INFO: Processing pre-safe import module hook gi from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-gi.py'. 6866 INFO: Loading module hook 'hook-clr.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'... 6898 INFO: Loading module hook 'hook-clr_loader.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'... 7070 INFO: Loading module hook 'hook-pycparser.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'... 7641 INFO: Processing pre-find module path hook distutils from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'. 7913 INFO: Loading module hook 'hook-distutils.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 7960 INFO: Loading module hook 'hook-distutils.util.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 8101 INFO: Loading module hook 'hook-setuptools.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 9741 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 10085 INFO: Loading module hook 'hook-pkg_resources.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 11273 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 11351 INFO: Loading module hook 'hook-sqlalchemy.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 15038 INFO: Loading module hook 'hook-certifi.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'... 15523 INFO: Loading module hook 'hook-anyio.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'... 16570 INFO: Analyzing hidden import 'tinyaes' 16570 INFO: Processing module hooks... 17023 INFO: Loading module hook 'hook-setuptools.msvc.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 17391 INFO: Loading module hook 'hook-difflib.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 17788 INFO: Loading module hook 'hook-sqlalchemy.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 18210 WARNING: Hidden import "pysqlite2" not found! 18210 WARNING: Hidden import "MySQLdb" not found! 18210 WARNING: Hidden import "psycopg2" not found! 18960 INFO: Loading module hook 'hook-sqlite3.py' from 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks'... 19492 INFO: Looking for ctypes DLLs 19523 INFO: Analyzing run-time hooks ... 19523 INFO: Including run-time hook 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_inspect.py' 19523 INFO: Including run-time hook 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py' 19523 INFO: Including run-time hook 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_multiprocessing.py' 19523 INFO: Including run-time hook 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_setuptools.py' 19538 INFO: Including run-time hook 'c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgres.py' 19554 INFO: Looking for dynamic libraries 1000 INFO: Extra DLL search directories (PATH): ['C:\Users\zxai\Desktop\cef\PPX-main\node_modules\.bin', 'C:\Users\zxai\AppData\Roaming\npm\node_modules\pnpm\dist\node-gyp-bin', 'C:\Program Files (x86)\Python\ Python37\Scripts\', 'C:\Program Files (x86)\Python\Python37\', 'C:\Windows\system32', 'C:\Windows', 'C:\Windows\System32\Wbem', 'C:\Windows\System32\WindowsPowerShell\v1.0\', 'C:\Windows\System32\OpenSSH\', 'C:\Program Files\CMake\bin', 'C:\Program Files\nodejs\', 'C:\Program Files (x86)\IncrediBuild', 'C:\Users\zxai\AppData\Local\Microsoft\WindowsApps', 'C:\Program Files\JetBrains\PyCharm 2023.2\bin', 'C:\Users\ \zxai\AppData\Roaming\npm', 'C:\Users\zxai\Desktop\cef\PPX-main\node_modules\.bin'] 21038 INFO: Looking for eggs 21038 INFO: Using Python library c:\users\zxai\desktop\cef\ppx-main\pyapp\pyenv\pyenv\scripts\python37.dll 21038 INFO: Found binding redirects: [] 21054 INFO: Warnings written to C:\Users\zxai\Desktop\cef\PPX-main\build\windows-folder\warn-windows-folder.txt 21210 INFO: Graph cross-reference written to C:\Users\zxai\Desktop\cef\PPX-main\build\windows-folder\xref-windows-folder.html 21257 INFO: checking PYZ 21257 INFO: Building PYZ because PYZ-00.toc is non existent 21257 INFO: Building PYZ (ZlibArchive) build\cache\PYZ-00.pyz 23148 INFO: Building PYZ (ZlibArchive) build\cache\PYZ-00.pyz completed successfully. 23179 INFO: checking PKG 23288 INFO: Copying icons from ['C:\Users\zxai\Desktop\cef\PPX-main\pyapp\spec\..\icon\logo.ico'] 23320 INFO: Writing RT_GROUP_ICON 0 resource with 146 bytes 23320 INFO: Writing RT_ICON 1 resource with 296 bytes 23320 INFO: Writing RT_ICON 2 resource with 744 bytes 23320 INFO: Writing RT_ICON 3 resource with 1640 bytes 23320 INFO: Writing RT_ICON 4 resource with 1384 bytes 23320 INFO: Writing RT_ICON 5 resource with 2216 bytes 23320 INFO: Writing RT_ICON 6 resource with 3752 bytes 23320 INFO: Writing RT_ICON 7 resource with 1128 bytes 23320 INFO: Writing RT_ICON 8 resource with 4264 bytes 23320 INFO: Writing RT_ICON 9 resource with 9640 bytes 23320 INFO: Writing RT_ICON 10 resource with 10012 bytes 23320 INFO: Copying 0 resources to EXE 23320 INFO: Embedding manifest in EXE 23320 INFO: Updating manifest in build\cache\PPX.exe.notanexecutable 23366 INFO: Updating resource type 24 name 1 language 0 23366 INFO: Appending PKG archive to EXE 23382 INFO: Fixing EXE headers 23554 INFO: Building EXE from EXE-00.toc completed successfully. 23554 INFO: checking COLLECT 23554 INFO: Building COLLECT because COLLECT-00.toc is non existent 23554 INFO: Building COLLECT COLLECT-00.toc 25695 INFO: Building COLLECT COLLECT-00.toc completed successfully
编译成功
但是运行之后 是空白