-1

I created a small game with pygame and wanted to share it with a friend, therefore i wanted to convert it into a .exe file. I'm using python 3.9.1, pygame 2.0.1 and pyinstaller 4.1 and my os is Win 10. After trying to run the created exe file no window appears and the console also only appears for a split second. I read, that pyinstaller may have problem with included files therefore i tested it with a simpler example:

import pygame
pygame.init()

surf = pygame.display.set_mode((600, 600))
while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            break
    
pygame.display.update()

here i have the same problem, that now window appears, when i run it via cmd the only error i get is <no python frame>

When testing other scripts i got the following error:

Fatal Python error: init_fs_encoding: failed to get the Python codec of   the filesystem encoding
Python runtime state: core initialized
LookupError: unknown encoding: utf-8

Current thread 0x00001f4c (most recent call first):
<no Python frame>

EDIT Using pyinstaller with python 3.8.6 seems to work

  • Can you share the command you use with pyinstaller? It should look something like this example: https://stackoverflow.com/questions/48757977/how-to-include-dependencies-from-venv-directory-when-running-pyinstaller-for-pro#answer-54645519 – spatzlsberger Jan 12 '21 at 13:59
  • The command i used was "pyinstaller --onefile scriptname.py", i used this a while back with python 3.7 and it worked fine back then (almost a year ago) – garrett323 Jan 12 '21 at 14:17

1 Answers1

0

I would say it's likely because when executed as an .exe, python.exe cannot find and import the pygame module. pyinstaller includes a flag where you can specify the location of your currently installed packages and then will include those in the .exe file it creates. That way, it always has access to those packages even if you move the .exe to a new computer.

You should try the following commands from the command line or powershell in the same directory as where your script is scriptname.py is located...

python -m venv .venv
.\.venv\Scripts\activate
pip install pygame
pyinstaller --onefile --paths .\.venv\Lib\site-packages scriptname.py

This will create a new virtual environment, which is a separate package directory than what is used in your overall system.

Activate the virtual environment.

Install pygame into that virtual environment

Then package your scriptname.py into an .exe which would include pygame.

  • Still getting the same "error" more or less. Now there is no error message when running the .exe from the console. I also tried adding the path to the virtual enviroment generated by pycharm. – garrett323 Jan 12 '21 at 15:22
  • I also tried using pyinstaller with a simple console script, that has no dependencies at all and it only briefly flashes the console. When I run it in cmd i get the following error: Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Python runtime state: core initialized LookupError: unknown encoding: utf-8 Current thread 0x00001f4c (most recent call first): – garrett323 Jan 12 '21 at 15:32
  • Check out https://stackoverflow.com/questions/54087049/fatal-python-error-initfsencoding-unable-to-load-the-file-system-codec to see if that helps. It may be due to your environment variables for python being set incorrectly. – spatzlsberger Jan 12 '21 at 16:11
  • Thanks for the help, i got it fixed by using python 3.8.6 – garrett323 Jan 12 '21 at 17:32