如何修复“警告:找不到隐藏的导入“pygame._view”!” 使用 PyInstaller 将 .py 转换为 .exe 时?

Abu*_*han 6 python pygame pyinstaller

这是我的代码的 MRE:

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 215, 0))

x_coordinate = 330
y_coordinate = 250
font = pygame.font.SysFont('comicsans', 30, False, False)
writing = font.render("this is a test", 1, (0,0,0))


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            screen.blit(writing,(x_coordinate, y_coordinate))
    pygame.display.update()
pygame.quit()
Run Code Online (Sandbox Code Playgroud)

现在,当我使用 cmd 或 IDLE 运行它时,它运行得很好。但是,当我使用 pyinstaller 使其可执行(我使用的命令是“pyinstaller --onefile {name of python file}”)时,cmd 窗口上会出现一个警告:“WARNING: Hidden import “pygame._view” not found! ” 我认为 pygame._view 是与 pygame 不同的模块,所以我添加import pygame._view了无济于事(我也尝试使用 pip 安装它,但不存在该名称的模块)。

现在,当我运行创建的可执行文件时,我收到一条错误消息,内容为“test.exe 已停止工作”。

我该如何解决这个问题?

小智 4

将“comicsans”替换为“comicsansms”。由于某些原因 pygame.Fonts 正在处理这种情况,即当作为 py 文件运行但不在 exe 中运行时,comicsans 不是系统字体