使用PyInstaller冻结Python现代应用程序



我正在尝试创建一个使用moderngl的Python项目的可执行程序。下面是一个最小的例子,它只是创建了一个窗口,并等待它退出:

import moderngl as mgl
import pygame as pg
pg.init()
screen = pg.display.set_mode((800, 600), pg.DOUBLEBUF | pg.OPENGL)
gl_ctx = mgl.create_context(require=330)
clock = pg.time.Clock()
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
clock.tick(30)

我跑

pyinstaller --onefile test_gl.py

在我的命令行,并得到一个.exe文件放在/dist目录。我所期望的是,当我运行。exe时,它会像运行python脚本时一样弹出空白窗口。相反,我得到错误消息:

Traceback (most recent call last):
File "test_gl.py", line 6, in <module>
File "modernglcontext.py", line 1619, in create_context
ModuleNotFoundError: No module named 'glcontext'
[7428] Failed to execute script 'test_gl' due to unhandled exception!

所以似乎一些必要的东西没有被打包到可执行文件中,但我不确定需要改变什么来包含它,如果它甚至是可能的。我以为PyInstaller会自动打包依赖项。

我有Python 3.10.4 PyInstaller 4.10, PyGame 2.1.2 (SDL 2.0.18, Python 3.10.4),和moderngl 5.6.4

原来二级依赖关系不一定是由PyInstaller添加的,所以使用--hidden-import glcontext解决了这个问题(或在.spec文件中将'glcontext'添加到hidden_imports)

最新更新