Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - Pyinstaller no window creation

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
..venvScriptsactivate
pip install pygame
pyinstaller --onefile --paths ..venvLibsite-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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...