我正在尝试获取一个Python文件,并使用Pyinstaller将其转换为exe文件。但是,当我这样做时,我收到一条错误消息:
Failed to execute python script.
这是pygame代码:
import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Auto Maze!")
myfont = pygame.font.SysFont("Arial", 13)
x, y = 168, 100
gameover = False
gameover2 = False
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
if gameover:
if button1.collidepoint(event.pos):
gameover = False
gameover2 = gameover
x, y = 168, 100
screen.fill((0, 0, 0))
"""try:
assert player.colliderect(wall1)
except AssertionError:
pass
except NameError:
pass
else:
death_screen = pygame.display.set_mode((400, 300))
button1 = pygame.draw.rect(death_screen, (0, 0, 255), (200, 200, 30, 30))
if donk:
break"""
if not gameover:
pressed = pygame.key.get_pressed()
if pressed[pygame.K_w]:
y -= 5
elif pressed[pygame.K_s]:
y += 5
elif pressed[pygame.K_a]:
x -= 5
elif pressed[pygame.K_d]:
x += 5
player = pygame.draw.rect(screen, (0, 255, 0), (x, y, 60, 60))
wall1 = pygame.draw.rect(screen, (255, 0, 0), (400, 0, -150, 300))
wall2 = pygame.draw.rect(screen, (255, 0, 0), (0, 0, 150, 300))
if player.colliderect(wall1):
gameover = True
elif player.colliderect(wall2):
gameover = True
else:
gameover = False
else:
button1 = pygame.draw.rect(screen, (0, 0, 255), (175, 100, 60, 30))
text = myfont.render("Try Again", False, (255, 0, 0))
screen.blit(text, (176, 107 ))
pygame.display.flip()
clock.tick(60)
quit()
这是我键入的用于转换文件的内容:pyinstaller --onefile -w "import pygame".py
另外,如果有替代Pyinstaller的更好的选择,请告诉我。我听说Pyinstaller并不是最好的。我的操作系统是Windows 10。
最佳答案
您需要将pyinstaller指向.py文件,而不是.py文件的第一行。
例如,如果您的文件名为example.py,则需要编写
pyinstaller --onefile -w example.py
关于python - 尝试转换py文件时,Pyinstaller中出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61433088/