如何修复“警告:隐藏导入”错误 pygame._view“未找到!”在我将 .py 程序转换为 .exe 后?

2024-01-11

将我的 .py 程序转换为 .exe 后,我的程序停止运行。我得到了WARNING: Hidden import information pygame._view "not found!"。我尝试导入该模块,但该模块不存在。我在互联网上搜索了解决方案,但没有发现任何有用的东西。很多回复都说这个问题在较新的pygame版本中不存在,其余的答案也没有帮助。但这是最新版本。有关 Pygame 和 Pyinstaller 以及我的代码的更多信息:https://repl.it/@Kadinus/MyGame https://repl.it/@Kadinus/MyGame!!! 在此站点上,我的 .exe 程序可以运行,但如果我直接在 PC 上启动它,它就无法运行。 pygame版本:1.9.6 py安装版本:3.5

import pygame

print ('Stage 1')

class Person():
    def __init__(self):
        self.x = 275
        self.Y = 275
        self.square = pygame.Rect(275, 275, 25, 25)
        self.font = pygame.font.Font(None, 40)
        #'self.massage = None' is written for example.
        self.massage = None

    def draw (self):
        pygame.draw.rect(window, (0, 0, 0), self.square, 3)

        text = self.font.render('Hi', 300, (0, 0, 0), (255, 200, 200))
        textpos = text.get_rect(x=10, y=10)
        window.blit(text, textpos)

pygame.init()

#Create the window and set its size.
window = pygame.display.set_mode (( 600, 600 ))
window.fill((255, 255, 255))

exit = False

print ('Stage 2')

#--------The problem is here--------

person = Person()

#-----------------------------------

print ('Stage 3')

while exit == False :
    pygame.time.delay(5)

        person.draw()

        #Check if the user closes the window.
        for event in pygame.event.get() :
                if event.type == pygame.QUIT :
                    exit = True

        pygame.display.update()

print ('Stage 4')

我希望代码能够顺利运行到最后。


事实上,我无法重现你的错误。但我很难冻结使用的应用程序pygame这也应该可以解决你的问题。

有时更好的方法是手动包含模块。首先,您需要排除您的模块exclude-module https://pyinstaller.readthedocs.io/en/v3.5/usage.html#what-to-bundle-where-to-search并将模块手动提供给最终的可执行文件Tree https://pythonhosted.org/PyInstaller/advanced-topics.html#the-toc-and-tree-classes班级。同样使用此方法,一些 Python 库会丢失,需要通过以下方式添加hidden-import https://pythonhosted.org/PyInstaller/when-things-go-wrong.html#listing-hidden-imports or Tree https://pythonhosted.org/PyInstaller/advanced-topics.html#the-toc-and-tree-classes。例如,我在这里添加了xml as Tree and queue as hidden-import.

import`. Use below spec file:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['script.py'],
             pathex=['C:\\Users\\Rahimi\\Desktop\\test'],
             binaries=[],
             datas=[],
             hiddenimports=['queue'],
             hookspath=[],
             runtime_hooks=[],
             excludes=['pygame'],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
a.datas += Tree("<python_path>/Lib/site-packages/pygame/", prefix= "pygame")
a.datas += Tree("<python_path>/lib/xml/", prefix= "xml")
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='script',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=False,
          runtime_tmpdir=None,
          console=True )

请记住根据您当前的环境编辑路径。最后,使用以下命令生成可执行文件:

pyinstaller script.spec
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复“警告:隐藏导入”错误 pygame._view“未找到!”在我将 .py 程序转换为 .exe 后? 的相关文章

随机推荐