使用PyInstaller打包PyWebView应用时提示需安装pythonnet的问题
PyWebView打包PyInstaller后提示缺少pythonnet的解决方法
问题详情
运行打包生成的.exe文件时出现错误:
Traceback (most recent call last): File "test.py", line 4, in <module> File "webview\__init__.py", line 245, in start File "webview\guilib.py", line 135, in initialize webview.errors.WebViewException: You must have pythonnet installed in order to use pywebview.
直接运行Python文件正常,极简测试代码:
import webview webview.create_window("Test", html="<h1>Hello</h1>") webview.start(debug=False)
使用的打包命令:
pyinstaller --onefile --windowed --hidden-import=clr test.py
已通过pip安装pythonnet和clr,但报错依旧。
解决方法
- 检查版本兼容性:确保pythonnet版本与PyWebView、Python版本匹配,推荐安装pythonnet>=2.5.2,同时确认Python是32位还是64位,所有依赖的位数必须一致。
- 扩展隐藏导入范围:修改打包命令,添加更多pythonnet相关模块的隐藏导入:
pyinstaller --onefile --windowed --hidden-import=clr --hidden-import=pythonnet --hidden-import=clr_loader test.py - 使用.spec文件手动配置:
- 生成spec文件:
pyinstaller --onefile --windowed test.py - 打开生成的
test.spec,在hiddenimports列表中添加['clr', 'pythonnet', 'clr_loader'] - 重新打包:
pyinstaller test.spec
- 生成spec文件:
- 确认环境一致性:检查打包使用的Python环境是否与运行.py文件的环境一致,确保pythonnet是安装在当前打包环境中,可使用绝对路径的pip安装(如
python -m pip install pythonnet)。 - 尝试非单文件打包:先去掉
--onefile参数打包成文件夹形式,验证是否能正常运行:
若文件夹版正常,再尝试单文件打包。pyinstaller --windowed --hidden-import=clr --hidden-import=pythonnet test.py
内容的提问来源于stack exchange,提问作者user32256289
相关产品推荐
相关产品推荐

