我有一个应用程序,我有两个可执行文件:Flask-SocketIO-Server和CefPython浏览器。我用PyInstaller捆绑了两个可执行文件。带有--onefile选项的Flask-Server和带有--onedir选项的cefpython,因为我无法使用--onefile。现在我决定只有两个代码的可执行文件(Flask和CEFpython),所以我的烧瓶服务器有代码来运行CEF图形用户界面:
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'dev':
print "Running Flask-SocketIO on dev mode"
else:
print "Running Flask-SocketIO on production mode"
path = os.getcwd()
gui_path = path + '\\display_react\\display_react.exe'
print 'Running Graphical User Interface...'
thread.start_new_thread(display_react.main, ()) # Baterias
print 'Initializing server'
socketio.run(app, debug=False)
代码工作正常,但是当我尝试使用--onefile选项将此代码与PyInstaller捆绑在一起时,生成的可执行文件不起作用会导致一些CEF依赖项。这里运行Pyinstaller时的错误:
在生产模式下运行Flask-SocketIO运行图形用户界面...初始化服务器[wxpython.py] CEF Python 57.1 [wxpython.py] Python 2.7.14 64bit [wxpython.py] wxPython 4.0.1 msw(phoenix)[0727 / 125110.576:错误:main_delegate.cc(684)]无法为en-US加载语言环境pak [0727 / 125110.576:错误:main_delegate.cc(691)]无法加载cef.pak [0727 / 125110.578:错误:main_delegate.cc (708)]无法加载cef_100_percent.pak [0727 / 125110.582:错误:main_delegate.cc(717)]无法加载cef_200_percent.pak [0727 / 125110.582:错误:main_delegate.cc(726)]无法加载cef_extensions.pak [0727 / 125110.648:错误:content_client.cc(269)]没有可用于id 20418的数据资源[0727/125110.648:错误:content_client.cc(269)]没有可用于id 20419的数据资源[0727/125110.650:错误:content_client .cc(269)]没有可用于ID 20420的数据资源[0727 / 125110.655:错误:content_client.cc(269)]没有可用于ID 20421的数据资源[0727/125110.656:错误:content_client.cc(269)]无数据资源可用于id 20422 [0727 / 125110.656:错误:content_client.cc(269)]没有可用于id 20417的数据资源[0727/125110.680:错误:extension_system.cc(72)]无法解析扩展清单。 C:\ Users \ Ricardo \ AppData \ Local \ Temp_MEI95~1 \ display_react.py:118:wxPyDeprecationWarning:调用已弃用的项目EmptyIcon。使用:class:
Icon
代替
这里是我正在使用的.spec文件:
# -*- mode: python -*-
block_cipher = None
def get_cefpython_path():
import cefpython3 as cefpython
path = os.path.dirname(cefpython.__file__)
return "%s%s" % (path, os.sep)
cefp = get_cefpython_path()
a = Analysis(['server.py'],
pathex=['C:\\Users\\Ricardo\\addvolt-scanning-tool\\backend'],
binaries=[],
datas=[('PCANBasic.dll', '.'), ('o.ico', '.')], #some dlls i need for flask
hiddenimports=['engineio.async_gevent'], #engineio hidden import for Flask usage
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas + [('locales/en-US.pak', '%s/locales/en-US.pak' % cefp, 'DATA')], # my try to fix that missing dependencies
name='server',
debug=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True )
编辑:已解决
感谢@cztomczak我得到了这个工作。问题不在PyInstaller上,而是在wxpython.py寻找区域设置,资源和子进程的方式上。虽然所有文件都在'temp / dir / _MEIxxx'上,但是wxpython正在可执行文件的目录中查找这些文件。所以我通知代码在临时目录中查找这些文件的方式是:
dir_temp = tempfile.gettempdir()
files = []
for i in os.listdir(dir_temp):
if os.path.isdir(os.path.join(dir_temp,i)) and '_MEI' in i:
files.append(i)
dir_temp = dir_temp + str(files[0])
dir_temp = os.path.join(dir_temp, str(files[0]))
dir_temp_locale = os.path.join(dir_temp, 'locales')
dir_temp_subprocess = os.path.join(dir_temp_subprocess, 'subprocess.exe')
print dir_temp
dir_temp = dir_temp.replace("\\", "\\\\")
print dir_temp
print dir_temp_locale
dir_temp_locale = dir_temp_locale.replace("\\", "\\\\")
print dir_temp_locale
dir_temp_supbprocess = dir_temp_subprocess.replace("\\", "\\\\")
print dir_temp_subprocess
...
settings = {'auto_zooming': '-2.5', 'locales_dir_path': dir_temp_locale, 'resources_dir_path': dir_temp, 'browser_subprocess_path': dir_temp_subprocess}
我不得不这样做,因为temp(_MEIxxxx)上创建的文件夹的名称总是在变化。可能我将来会遇到问题,因为如果应用程序崩溃,_MEIxx文件夹将不会被删除,如果我尝试重新运行可执行文件,这段代码将有两个_MEI文件夹,可能根本不会工作,直到有人清理临时目录。
所以,恢复...要在一个文件中捆绑应用程序: - 在Python27 / envs / libs / site-package / Pyinstaller / hooks上粘贴hook-cefpython3.py(在包上可用) - 使用--onefile选项运行Pyinstaller - 告诉cefpython代码,其中语言环境,资源和子进程是(locale_dir_path,resource_dir_path,browser_subprocess_path)
我猜你得到的错误是因为你的spec文件没有包含所有必要的CEF二进制文件。有一个官方的pyinstaller示例,您可以使用和修改以使用--onefile选项:https://github.com/cztomczak/cefpython/blob/master/examples/pyinstaller/README-pyinstaller.md
我有一个类似的问题,发现使用_MEIPASS环境变量是一个更优雅的解决方案。
import cefpython
import os
import sys
if hasattr(sys, '_MEIPASS'):
# settings when packaged
settings = {'locales_dir_path': os.path.join(sys._MEIPASS, 'locales'),
'resources_dir_path': sys._MEIPASS,
'browser_subprocess_path': os.path.join(sys._MEIPASS, 'subprocess.exe'),
'log_file': os.path.join(sys._MEIPASS, 'debug.log')}
else:
# settings when unpackaged
settings = {}
cefPython.Initialize(settings=settings)