gpt4 book ai didi

python-3.x - pyinstaller 创建的可执行文件不运行

转载 作者:行者123 更新时间:2023-12-02 07:19:06 25 4
gpt4 key购买 nike

我正在 pygame 上创建一个数独求解器,现在想为该项目制作一个可执行文件,因为您不能指望用户了解编程、虚拟环境等
我在我的虚拟环境中安装了 PyInstaller,在 pygame 中,并使用了以下命令:

pyinstaller --onefile -w main.py
有效。
但是,我无法在 dist 文件夹中运行可执行文件:(
当我尝试单击可执行文件时,粘贴时没有任何 react 。使用终端,我收到以下错误:
./main
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "main.py", line 1, in <module>
import pygame
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "/home/lucas1809/Área de Trabalho/Projetos/sudokey/venv/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "PyInstaller/__init__.py", line 68, in <module>
File "setuptools-40.8.0-py3.6.egg/pkg_resources/__init__.py", line 481, in get_distribution
File "setuptools-40.8.0-py3.6.egg/pkg_resources/__init__.py", line 357, in get_provider
File "setuptools-40.8.0-py3.6.egg/pkg_resources/__init__.py", line 900, in require
File "setuptools-40.8.0-py3.6.egg/pkg_resources/__init__.py", line 786, in resolve
pkg_resources.DistributionNotFound: The 'PyInstaller' distribution was not found and is required by the application
[3920] Failed to execute script main
我不认为 main.py 代码是必要的,因为它在 pycharm 中执行时运行良好。但是,如果我错了,我会发布代码
如果有人可以帮助我解决这个问题,我将不胜感激。如果你理解有问题,或者我不清楚。请问。
提前致谢 :)
编辑:这是“main.py”代码:
import pygame, os, PyInstaller
from pygame.locals import *
from board import Board
from button import Button

# Initialization and screen surface loading
pygame.init()
screen = pygame.display.set_mode()
screenSize = pygame.display.get_surface().get_size()
width = screenSize[0]
height = screenSize[1]
pygame.display.set_caption("Sudokey: Sudoku's Solver")

# Image and music loading
bgMenu = pygame.image.load("background/sudokey2Menu.png")
bgMenu = pygame.transform.scale(bgMenu, (width, height - 30))
bgStart = pygame.image.load("background/sudokeyCustom.png")
bgStart = pygame.transform.scale(bgStart, (width - 40, height - 55))
pygame.mixer.pre_init()
pygame.mixer.init()
pygame.mixer.music.load("musica/lullabyGhostInYourPiano.mp3")
pygame.mixer.music.play(-1)
click = pygame.mixer.Sound("sons/click.ogg")

# Default screen and game state
running = 1
menu = 1
start = 0
credit = 0

# Mouse logic to detect click
currentSquare = (9, 9)
clickedCell = None

# Creating board using class "Board"
tabuleiro = Board()

# Creating menu buttons using class "Button"
buttonStart = Button(400, 186, 530, 90)
buttonTutorial = Button(400, 325, 530, 90)
buttonOptions = Button(400, 464, 530, 90)
buttonCredits = Button(400, 603, 530, 90)

# Creating start buttons using class "Button"
buttonSolve = Button(898, 40, 380, 80)
buttonReset = Button(898, 159, 380, 80)
buttonGoBack = Button(898, 279, 380, 80)
buttonOptionsStart = Button(898, 398, 380, 80)

# Font loading
font = pygame.font.Font("freesansbold.ttf", 30)


# Visually updates the board
def drawGrid(board):
for i in range(9):
for j in range(9):
if (board[i][j]):
text = font.render(str(board[i][j]), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (j * 90 + 45, i * 80 + 45)
screen.blit(text, textRect)

# Plays music based on input
def jukebox(number):
if number == 0:
pygame.mixer.music.stop()
elif number == 1:
pygame.mixer.music.load("musica/lullabyGhostInYourPiano.mp3")
pygame.mixer.music.play(-1)
elif number == 2:
pygame.mixer.music.load("musica/adventureGhostInYourPiano.mp3")
pygame.mixer.music.play(-1)
elif number == 3:
pygame.mixer.music.load("musica/liebestrau.mp3")
pygame.mixer.music.play(-1)
elif number == 4:
pygame.mixer.music.load("musica/Kiss_the_Sky.mp3")
pygame.mixer.music.play(-1)
elif number == 5:
pygame.mixer.music.load("musica/Lullaby.mp3")
pygame.mixer.music.play(-1)
elif number == 6:
pygame.mixer.music.load("musica/Gentle_Breeze.mp3")
pygame.mixer.music.play(-1)
elif number == 7:
pygame.mixer.music.load("musica/Eternal_Hope.mp3")
pygame.mixer.music.play(-1)
elif number == 8:
pygame.mixer.music.load("musica/Pressure.mp3")
pygame.mixer.music.play(-1)
elif number == 9:
pygame.mixer.music.load("musica/01 To the Moon - Main Theme.mp3")
pygame.mixer.music.play(-1)


while running:
while menu:
pygame.display.flip()
screen.blit(bgMenu, (0, 0))
for event in pygame.event.get():
if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
running = 0
menu = 0
elif event.type == pygame.KEYDOWN and event.key == pygame.K_s:
start = 1
menu = 0
if event.type == pygame.MOUSEBUTTONUP:
x, y = pygame.mouse.get_pos()

if buttonStart.isOn(x, y):
#click.play()
#click.stop()

print(x, y)
menu = 0
start = 1

elif buttonTutorial.isOn(x, y):
print(x, y)
print('tutorial')
menu = 0
start = 1

elif buttonOptions.isOn(x, y):
print(x, y)
print('Options')
menu = 0
start = 1

elif buttonCredits.isOn(x, y):
print(x, y)
print('Credits')
menu = 0
start = 1

if (event.type == pygame.KEYDOWN):
if (pygame.K_0 <= event.key <= pygame.K_9):
number = int(event.unicode)
jukebox(number)

while start:
pygame.display.flip()
screen.blit(bgStart, (0, 0))
drawGrid(tabuleiro.tabuleiro)
for event in pygame.event.get():
if event.type == pygame.QUIT:
print('stopping')
running = 0
start = 0

elif event.type == pygame.KEYDOWN and (event.key == pygame.K_m or event.key == pygame.K_ESCAPE):
start = 0
menu = 1

if (event.type == pygame.MOUSEBUTTONUP):
coords = pygame.mouse.get_pos()
col = coords[1] // 80
line = coords[0] // 90
clickedCell = (line, col)

if (event.type == pygame.KEYDOWN):
if (clickedCell != None):
if (pygame.K_0 <= event.key <= pygame.K_9):
line = clickedCell[1]
col = clickedCell[0]
number = int(event.unicode)
if 0 <= line <= 8 and 0 <= col <= 8:
tabuleiro.setCell(line, col, number)
clickedCell = None

if (event.type == pygame.KEYDOWN):
if event.key == pygame.K_s:
tabuleiro.findFirst()
tabuleiro.solve()
elif event.key == pygame.K_r:
tabuleiro.reset()


if event.type == pygame.MOUSEBUTTONUP:
x, y = pygame.mouse.get_pos()
print(x, y)

if buttonSolve.isOn(x, y):
print('solving')
tabuleiro.solve()

elif buttonReset.isOn(x, y):
tabuleiro.reset()
tabuleiro.show()

elif buttonGoBack.isOn(x, y):
start = 0
menu = 1

elif buttonOptionsStart.isOn(x, y):
start = 0
menu = 1

pygame.quit()

Obs: Button 和 Board 都是我用来解决游戏的类
编辑2:先前的错误已修复! :)
现在,我又得到了一个,但这一次,我可以执行该文件,但它会立即关闭
这是错误,现在:
./main
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "main.py", line 51, in <module>
font = pygame.font.Font("freesansbold.ttf", 30)
File "pygame/pkgdata.py", line 50, in getResource
File "setuptools-40.8.0-py3.6.egg/pkg_resources/__init__.py", line 1134, in resource_exists
File "setuptools-40.8.0-py3.6.egg/pkg_resources/__init__.py", line 1404, in has_resource
File "setuptools-40.8.0-py3.6.egg/pkg_resources/__init__.py", line 1457, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
[3816] Failed to execute script main

看起来它与pygame中的字体有关,但我不明白为什么。代码在 pycharm 中完美运行。
也许我的 pyinstaller 使用了错误版本的 pygame,并且无法识别该方法?对我来说没有意义,但我不知道是什么原因造成的
提前感谢(再次)。除了这里,我在任何地方都找不到帮助

最佳答案

尝试像这样放置完整的文件名

pygame.font.Font('./freesansbold.ttf')

关于python-3.x - pyinstaller 创建的可执行文件不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62777647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com