Cioty
Cioty

Reputation: 33

How to fix the "WARNING: Hidden import" error pygame._view "not found!" after I turned my .py program into .exe?

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.

Upvotes: 2

Views: 1520

Answers (1)

Masoud Rahimi
Masoud Rahimi

Reputation: 6051

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.

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

Upvotes: 2

Related Questions