我的带有kivy GUI的python脚本在Pycharm或Spyder IDE中平稳运行。使用pyinstaller创建EXE(Windows(后,EXE可以在GUI出现的情况下运行,而不会出现任何问题。但是,当我单击运行plyer.filechooser.open_file()
或plyer.filechooser.choose_dir()
的按钮时,会出现以下错误:
Traceback (most recent call last):
File "plyerutils.py", line 96, in _ensure_obj
ModuleNotFoundError: No module named 'plyer.platforms'
[INFO ] [Base ] Leaving application in progress...
我刚刚意识到pyinstaller在";dist";目录包含我的脚本中使用过的包,如pandas、numpy、pyexcelerate等,但不是plyer。这很奇怪,因为我的脚本一开始就有明确的import plyer
。
我的pyinstaller规范文件如下所示。我使用plyer选择文件的原因是因为它的大小很小,而PyQt5会创建巨大的EXE大小,而kivy FileChooser不会显示用户计算机中所有可用的硬盘驱动器(如果有人知道这方面的解决方案,那就太好了(。
import os
from os.path import join
from kivy import kivy_data_dir
from kivy.deps import sdl2, glew
from kivy.tools.packaging import pyinstaller_hooks as hooks
block_cipher = None
kivy_deps_all = hooks.get_deps_all()
kivy_factory_modules = hooks.get_factory_modules()
# list of data filepath (your *.kv, image files, kivymd files) to copy to the output directory
datas = [('./gui.kv', '.'),
('./venv/Lib/site-packages/kivymd', 'kivymd')
]
# list of modules to exclude from Analysis
excludes_a = ['Tkinter', '_tkinter', 'twisted', 'docutils', 'pygments']
# list of hiddenimports
hiddenimports = kivy_deps_all['hiddenimports'] + kivy_factory_modules
# binary data
sdl2_bin_tocs = [Tree(p) for p in sdl2.dep_bins]
glew_bin_tocs = [Tree(p) for p in glew.dep_bins]
bin_tocs = sdl2_bin_tocs + glew_bin_tocs
# assets
kivy_assets_toc = Tree(kivy_data_dir, prefix=join('kivy_install', 'data'))
# source_assets_toc = Tree("images", prefix="images")
# assets_toc = [kivy_assets_toc, source_assets_toc]
assets_toc = [kivy_assets_toc]
tocs = bin_tocs + assets_toc
a = Analysis(['main.py'],
pathex=[os.getcwd()],
binaries=None,
datas=datas,
hiddenimports=hiddenimports,
hookspath=[],
runtime_hooks=[],
excludes=excludes_a,
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe1 = EXE(pyz,
a.scripts,
name='main', # name of output *.exe file
exclude_binaries=True,
# icon=join('images', 'mywinapp.ico'),
debug=False,
strip=False,
upx=True,
console=True) # set to console-based, NOT window-based.
coll = COLLECT(exe1,
a.binaries,
a.zipfiles,
a.datas,
*tocs,
strip=False,
upx=True,
name='Parser')
我也遇到了同样的问题。
我的修复方法是在使用pyinstaller 创建exe时添加以下参数
--hidden-import plyer.platforms.win.notification
将以下隐藏导入添加到规范文件
a = Analysis(
...
hiddenimports=['plyer.platforms.win.filechooser'],
...
此问题是由缺少隐藏导入引起的。尝试包括";平台专用模块";就像brsaylor在Windows通知中所做的那样-NotImplementedError:找不到可用的实现#485:
https://github.com/kivy/plyer/issues/485#issuecomment-457880444
我在macos上的FileChooser遇到了类似的问题,我通过在规范文件中向
hiddenimports
添加特定于平台的模块来解决了这个问题。对于这个问题,更改将类似于:a = Analysis( ... hiddenimports=['plyer.platforms.win.notification'], ...