Python Forum
Problems with executing .exe file (was .py)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with executing .exe file (was .py)
#1
I turned a .py file into .exe file. It works just fine on python but won't run as a .exe file. I don't know why. When I click on it, it opens up a screen, but says "Failed to execute script HyperSpace!" I don't know what to do. I used the method - https://www.youtube.com/watch?v=OZSZHmWSOeM - because I just could get cx_Freeze to work. Here's the code for my game. Quick disclaimer - If you run it... brace yourself to become blind.

#Imports
import pygame
import time
import random
from functools import partial

pygame.init()



#Variables/Shortcuts
pd = pygame.display
fps = 100
display_width = 1000
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
darker_red = (200,0,0)
green = (0,255,0)
blue = (0,0,255)
ColorList = [black, red, green, blue]
CustomColor1 = (48,150,140)
CustomColor2 = (36,112.5,105)
CustomColor3 = (0,0,0)
Unique_Color = (190,140,210)
Score = 0
highscore = 8050
carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCar.png')
carImgLeft = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCarLeft.png')
carImgRight = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCarRight.png')
car_width = 55
car_height = 50

#Game Start up
gD = pygame.display.set_mode((display_width,display_height))

#Game Conifgurations
pd.set_caption('HyperSpace!')
clock = pygame.time.Clock()
gD.fill(white)

#Functions
def car(img, x, y):
    gD.blit(img, (x,y))

def CrashDisplay(score, val):
    gD.fill(random.choice(ColorList))
    if val == True:
        message_display('You Crashed. Your Score was %s' %score)
        time.sleep(10)
    else:
        message_display2('Your score is - %s - Highscore is %s' %(score, highscore))
    
def message_display(text):
    font = pygame.font.Font('freesansbold.ttf', 50)
    textSurf, TextRect = text_objects(text, font)
    TextRect.center = ((display_width/2),(display_height/2))
    gD.blit(textSurf, TextRect)

    pygame.display.update()

    time.sleep(10)

def message_display2(text):
    font = pygame.font.Font('freesansbold.ttf', 30)
    textSurf, TextRect = text_objects(text, font)
    TextRect.center = ((display_width/2),(20))
    gD.blit(textSurf, TextRect)
    pygame.display.update()
    
def ScoreKeeper(pts):
    global Score
    Score += pts

def text_objects(text, font):
    textSurface = font.render(text, True, Unique_Color)
    return textSurface, textSurface.get_rect()

def obstacle(obstX, obstY, obstW, obstH, color):
    pygame.draw.rect(gD, color, [obstX, obstY, obstW, obstH])
    
def StartScreen():
    intro = True

    while intro == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        gD.fill(white)
        font = pygame.font.Font('freesansbold.ttf', int(display_width/10))
        textSurf, TextRect = text_objects('HyperSpace!', font)
        TextRect.center = ((display_width/2),(display_height/2))
        gD.blit(textSurf, TextRect)
        Button(round(display_width * .3), round(display_height * .7), round(display_width * .4), round(display_height * .2), CustomColor1, CustomColor2, 'Start!', 50, blue, 'freesansbold.ttf', game_loop)
        Button(round(display_width * .8), round(display_height * .8), round(display_width * .2), round(display_height * .1), red, darker_red, "Quit", 20, blue, 'freesansbold.ttf', quit, pygame.quit)
        pygame.display.update()
        clock.tick(15)

def Button(Butx, Buty, Butx2, Buty2, Butcolor, ShadowColor, text, textsize, textcolor, textFont, command=None, command2=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    smallText = pygame.font.Font(textFont, textsize)
    textSurf, textRect = text_objects(text, smallText)
    textRect.center = ((Butx + (Butx2/2)), Buty + (Buty2/2))
    pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2))
    gD.blit(textSurf, textRect)
    if Butx + Butx2 > mouse[0] > Butx and Buty + Buty2 > mouse[1] > Buty: 
        pygame.draw.rect(gD, ShadowColor, (Butx, Buty, Butx2, Buty2))
        gD.blit(textSurf, textRect)
        if click[0] == 1:
            if command != None:
                command()
            if command2 != None:
                command2()
    else:
        pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2))
        gD.blit(textSurf, textRect)

def Pause():
    pause = True
    message_display('Game is Paused')
    while pause:
         for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    pause = False
#Main
def game_loop():
    global carImg
    play = True
    global Score
    x = (display_width * 0.5)
    y = (display_height * 0.8)

    x_change = 0

    obst_startX = random.randint(0, display_width)
    obst_startY = -600
    obst_speed = 2
    min_width = 100
    max_width = 100
    obst_width = 0
    obst_height = 0
    obst_height = random.randint(round(display_width/8), round(display_width/2))
    obst_color = random.choice(ColorList)
    
    while play:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                StartScreen()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                    carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCarLeft.png')
                elif event.key == pygame.K_RIGHT:
                    x_change = +5
                    carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCarRight.png')
                elif event.key == pygame.K_p:
                    Pause()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
                    carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCar.png')
                
        Score += 1
        x += x_change

        CrashDisplay(Score, False)
        obstacle(obst_startX, obst_startY, random.randint(min_width, max_width), obst_height, obst_color)
        
        obst_startY += obst_speed
        car(carImg, x, y)

        if x > display_width - 76 or x < -5:
            play = False

        if obst_startY > display_height:
            obst_startY = 0 - obst_height
            obst_startX = random.randint(0, display_width)
            obst_width = random.randint(min_width, max_width)
            obst_height = random.randint(round(display_width/8), round(display_width/2)) 
            obst_color = random.choice(ColorList)
            min_width += 10
            max_width += 11
        if y <= obst_startY + obst_height:
            if x > obst_startX and x < obst_startX + obst_width or x + car_width > obst_startX and x + car_width < obst_startX + obst_width:
                CrashDisplay(Score, True)
                StartScreen()


        if Score == 2000:
            obst_speed = 3

        elif Score == 4000:
            obst_speed = 4

        elif Score == 6000:
            obst_speed = 5

        elif Score == 8000:
            obst_speed = 6

        elif Score == 10000:
            obst_speed = 7

        elif Score == 12000:
            obst_speed = 8

        elif Score == 14000:
            obst_speed = 9

        elif Score == 16000:
            obst_speed = 10

        elif Score == 18000:
            obst_speed = 11

        elif Score == 20000:
            obst_speed = 12
        pygame.display.update()
        clock.tick(fps)
StartScreen()
pygame.quit()
quit()
Reply
#2
The error message you got is a generic one, some error/exception happened. It could be something like a missing DLL, or script not able to find some graphics library (path...) etc.
Logging to file or console is one way to attempt debugging. But I don't really know PyGame, there may be more appropriate ways for this.
Reply
#3
pyinstaller is now the preferred method of building an exe. This is more updated and you probably would not have so many problems. Since that program uses pyinstaller, i guess its OK. I never heard of it before now.

Did you include additional directories for you font directory and image directories?

Before you hit convert .py to .exe button, post what is in that current command (all of it). We need to know how you are building your exe. Also post your output that you get from that exe being built (all of it).

You can give the argument --debug to write progress messages in the output. Do this before copying the output for us.

I would start small and work your way up when building exe files form py. For example write a basic hello world and see if that works. If all goes well, then try to import pygame and make a window. If that builds into an exe, then add directories for resources. Form there try your full program. This will easily and quickly pinpoint if there is an issue with python building into an exe or pyinstaller config, pygame into an exe, or a directory conflict issue. There are a lot of issues that can arise when building exe's from python. Its not a one problem fix.

There is also a section for when things go wrong in pyinstaller that you can read through
https://pythonhosted.org/PyInstaller/whe...wrong.html


I had a py2exe setup script ready to go for pygame specifically. It had some special things i had to do with the setup script though. However apparently py2exe is no longer updated past python3.4-ish. You could try it for later versions, but not sure if there would be problems or not now. I havent done it in a long time.
Recommended Tutorials:
Reply
#4
The Current Command says - pyinstaller -y -F -w "C:\Users\Chadd\Desktop\HyperSpace!\HyperSpace!.py" - before I push it. I picked "One File" and "Window Based". The output when I click "Convert .PY to .EXE" is -
Output:
Running auto-py-to-exe v2.5.2 Building in the current instances temporary directory at C:\Users\Chadd\AppData\Local\Temp\tmpx3odjzzh To get a new temporary directory, restart this application Recursion Limit is set to 5000 Executing: pyinstaller -y -F -w "C:\Users\Chadd\Desktop\HyperSpace!\HyperSpace!.py" 37166 INFO: PyInstaller: 3.4 37166 INFO: Python: 3.7.2 37166 INFO: Platform: Windows-8.1-6.3.9600-SP0 37167 INFO: wrote C:\Users\Chadd\AppData\Local\Temp\tmpx3odjzzh\HyperSpace!.spec 37168 INFO: UPX is not available. 37170 INFO: Extending PYTHONPATH with paths ['C:\\Users\\Chadd\\Desktop\\HyperSpace!', 'C:\\Users\\Chadd\\AppData\\Local\\Temp\\tmpx3odjzzh'] 37170 INFO: checking Analysis 37170 INFO: Building Analysis because Analysis-00.toc is non existent 37170 INFO: Initializing module dependency graph... 37174 INFO: Initializing module graph hooks... 37240 INFO: Analyzing base_library.zip ... 43096 INFO: running Analysis Analysis-00.toc 43147 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by C:\Users\Chadd\AppData\Local\Programs\Python\Python37-32\python.exe 44287 INFO: Caching module hooks... 44294 INFO: Analyzing C:\Users\Chadd\Desktop\HyperSpace!\HyperSpace!.py 45766 INFO: Processing pre-find module path hook distutils 47969 INFO: Processing pre-safe import module hook setuptools.extern.six.moves 49432 INFO: Processing pre-find module path hook site 49448 INFO: site: retargeting to fake-dir 'C:\\Users\\Chadd\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\PyInstaller\\fake-modules' 55019 INFO: Loading module hooks... 55019 INFO: Loading module hook "hook-distutils.py"... 55080 INFO: Loading module hook "hook-encodings.py"... 55168 INFO: Loading module hook "hook-lib2to3.py"... 55218 INFO: Loading module hook "hook-numpy.core.py"... 56187 INFO: Loading module hook "hook-numpy.py"... 56188 INFO: Loading module hook "hook-pkg_resources.py"... 56590 INFO: Processing pre-safe import module hook win32com 57005 INFO: Loading module hook "hook-pycparser.py"... 57006 INFO: Loading module hook "hook-pydoc.py"... 57006 INFO: Loading module hook "hook-pygame.py"... 57007 WARNING: Hidden import "pygame._view" not found! 57007 INFO: Loading module hook "hook-pythoncom.py"... 57377 INFO: Loading module hook "hook-pywintypes.py"... 57804 INFO: Loading module hook "hook-scipy.py"... 57919 INFO: Loading module hook "hook-setuptools.py"... 58642 INFO: Loading module hook "hook-sysconfig.py"... 58658 INFO: Loading module hook "hook-win32com.py"... 59328 INFO: Loading module hook "hook-xml.etree.cElementTree.py"... 59342 INFO: Loading module hook "hook-xml.py"... 59519 INFO: Looking for ctypes DLLs 59552 INFO: Analyzing run-time hooks ... 59559 INFO: Including run-time hook 'pyi_rth_pkgres.py' 59580 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py' 59590 INFO: Including run-time hook 'pyi_rth_multiprocessing.py' 59606 INFO: Looking for dynamic libraries 75448 INFO: Looking for eggs 75449 INFO: Using Python library C:\Users\Chadd\AppData\Local\Programs\Python\Python37-32\python37.dll 75449 INFO: Found binding redirects: [] 75461 INFO: Warnings written to C:\Users\Chadd\AppData\Local\Temp\tmpx3odjzzh\build\HyperSpace!\warn-HyperSpace!.txt 75611 INFO: Graph cross-reference written to C:\Users\Chadd\AppData\Local\Temp\tmpx3odjzzh\build\HyperSpace!\xref-HyperSpace!.html 75676 INFO: checking PYZ 75676 INFO: Building PYZ because PYZ-00.toc is non existent 75676 INFO: Building PYZ (ZlibArchive) C:\Users\Chadd\AppData\Local\Temp\tmpx3odjzzh\build\HyperSpace!\PYZ-00.pyz 77250 INFO: Building PYZ (ZlibArchive) C:\Users\Chadd\AppData\Local\Temp\tmpx3odjzzh\build\HyperSpace!\PYZ-00.pyz completed successfully. 77274 INFO: checking PKG 77275 INFO: Building PKG because PKG-00.toc is non existent 77275 INFO: Building PKG (CArchive) PKG-00.pkg 93195 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 93203 INFO: Bootloader C:\Users\Chadd\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PyInstaller\bootloader\Windows-32bit\runw.exe 93203 INFO: checking EXE 93203 INFO: Building EXE because EXE-00.toc is non existent 93203 INFO: Building EXE from EXE-00.toc 93216 INFO: Appending archive to EXE C:\Users\Chadd\AppData\Local\Temp\tmpx3odjzzh\application\HyperSpace!.exe 93255 INFO: Building EXE from EXE-00.toc completed successfully. Moving project to: C:\Users\Chadd\Desktop\HyperSpace!/ Complete.
Reply
#5
There is an option in that program to add a directory. I would add this
C:/Users/Chadd/Desktop/Ayden Stuff/ to your project so that it can include those resources. That or move those resources to C:\Users\Chadd\Desktop\HyperSpace!/ and load them in your script as carImg = pygame.image.load('RaceCar.png') instead of carImg = pygame.image.load('C:/Users/Chadd/Desktop/Ayden Stuff/RaceCar.png')

I would also remove the -w flag during the debugging process and see what the console says of possible errors when you actually run the exe.
Recommended Tutorials:
Reply
#6
Taking away the '-w' changes it to console based and i added a folder with all the pictures for the gam. Current command now looks like this -
Output:
pyinstaller -y -F --add-data "C:/Users/Chadd/Desktop/HyperSpace Stuff";"HyperSpace Stuff/" "C:\Users\Chadd\Desktop\HyperSpace!\HyperSpace!.py"

I found an error after editing a few things and it's line 92. The error is -
Error:
Traceback (most recent call last) File "HyperSpace!.py", line 225, in <module> StartScreen() File "HyperSpace!.py", line 92, in StartScreen font = pygame.font.Font('freesandbold.ttf', int(display_width/10)) File "site-packages\pygame\pkgdata.py", line 50, in getResource File "site-packages\pkg_resources\__init__.py", line 1132, in resource_exists File "site-packages\pkg_resources\__init__.py", line 1402, in has_resource File "site-packages\pkg_resources\__init__.py", line 1455, in _has NotImplementError: Can't perform this operaion for unregistered loader type [5692] Failed to execute script HyperSpace!
The error only diaplays for a second so I snipped it and hand-typed it. Hope I didn''t type any mistakes. The actual lines that it references from HyperSpace!.py are different than what i typed above. I wrote the lines that matched my code above
Reply
#7
are you expecting freesansbold.ttf to be on your system or do you actually have a freesansbold.ttf font file in the same directory of your startup script? As is you are trying to load freesansbold.ttf file from your root directory. I am not sure if this is what you intended or not?

pygame.font.Font is for loading your own font files taking a path to the font file, where pygame.font.SysFont is for loading system fonts and usually does not need a path. Either way, due to the fact this seems to be a resource/font issue....I would simply try a different font.

You can get a list of system wide fonts via
sorted(pygame.font.get_fonts())
Make sure to use SysFont and Arial is usually a safe one as some systems dont have freesansbold but only freesans. Or the safest approach is to use your own font.
Recommended Tutorials:
Reply
#8
Thank you
Reply
#9
which was the issue?
Recommended Tutorials:
Reply
#10
Sorry for late reply. It wasn't loading the font I was using and didn't worked. But it loaded the arial font
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020