【问题标题】:How to fix the "WARNING: Hidden import" error pygame._view "not found!" after I turned my .py program into .exe?如何修复“警告:隐藏导入”错误 pygame._view“未找到!”在我把我的 .py 程序变成 .exe 之后?
【发布时间】:2019-11-24 14:27:03
【问题描述】:

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

我希望代码运行到最后没有错误。

【问题讨论】:

  • 代码中突出显示问题
  • repl.it 真的编译并执行 .exe 文件,还是只是在 python 解释器中运行你的main.py 文件? MyGame_Test.exe 是如何创建的?

标签: python pygame pyinstaller


【解决方案1】:

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

有时更好的方法是手动包含您的模块。首先,您需要使用exclude-module 排除您的模块,并手动将模块提供给带有Tree 类的最终可执行文件。同样使用这种方法,一些 Python 库会丢失,需要通过 hidden-importTree 添加。例如,在这里我将xml 添加为Tree,将queue 添加为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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 2020-10-28
    • 2017-04-04
    • 2017-09-12
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多