如何修复“警告:隐藏的导入"错误 pygame._view “未找到!"在我将我的 .py 程序变成 .exe 之后? [英] How to fix the "WARNING: Hidden import" error pygame._view "not found!" after I turned my .py program into .exe?

查看:58
本文介绍了如何修复“警告:隐藏的导入"错误 pygame._view “未找到!"在我将我的 .py 程序变成 .exe 之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After converting my .py program to .exe, my program stops running. I get the WARNING: Hidden import information pygame._view "not found!". I tried to import the module but that does not exist. I have searched for solutions on the internet but I have found nothing useful. Many replies said this problem in newer pygame versions did not exist, and the rest of the answers did not help. But this is the newest version. More information about Pygame and Pyinstaller and about my code: https://repl.it/@Kadinus/MyGame !!! On this site, my .exe program works but if I start it directly on my PC it does not work. Pygame version: 1.9.6 Pyinstall version: 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')

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

I expect the code to run to the end without errors.

推荐答案

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

Actually, I can't reproduce your error. But I had a hard time to freeze apps that uses pygame and this should fix your problem too.

有时更好的方法是手动包含您的模块.首先,您需要使用 exclude-module 并使用 Tree 类.同样使用这种方法,一些 Python 库会丢失,需要通过 hidden-import.例如,在这里我添加了 xml 作为 Treequeue 作为 hidden-import.

Sometimes a better way is to include your module manually. For that first, you need to exclude your module with exclude-module and feed the module manually to the final executable with Tree class. Also with this method, some Python libs would miss and need to be added either by hidden-import or Tree. For example, in here I've added 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 )

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

Remember to edit path based on your current environment. Finally, generate your executable with:

pyinstaller script.spec

这篇关于如何修复“警告:隐藏的导入"错误 pygame._view “未找到!"在我将我的 .py 程序变成 .exe 之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆