You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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文件手动配置:
    1. 生成spec文件:pyinstaller --onefile --windowed test.py
    2. 打开生成的test.spec,在hiddenimports列表中添加['clr', 'pythonnet', 'clr_loader']
    3. 重新打包:pyinstaller test.spec
  • 确认环境一致性:检查打包使用的Python环境是否与运行.py文件的环境一致,确保pythonnet是安装在当前打包环境中,可使用绝对路径的pip安装(如python -m pip install pythonnet)。
  • 尝试非单文件打包:先去掉--onefile参数打包成文件夹形式,验证是否能正常运行:
    pyinstaller --windowed --hidden-import=clr --hidden-import=pythonnet test.py
    
    若文件夹版正常,再尝试单文件打包。

内容的提问来源于stack exchange,提问作者user32256289

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 16:04:52