'I decided to build my pygame project from .py format to exe. When i tried to run the script, i get an error
My project has a lot of files, this application starts with start.py script. When i run this script, i get an "Import Error". I using a pyinstaller module for build my project to exe.
Code
import json
import os
import pygame as pg
from about import About
from code import App
class Menu:
def __init__(self):
pg.init()
pg.mixer.init()
self.file_dir = os.path.dirname(__file__)
self.json_conf_dir = os.path.join(self.file_dir)
self.font_dir = os.path.join(self.file_dir)
self.img_dir = os.path.join(self.file_dir)
with open(os.path.join(self.json_conf_dir, "file_paths.json")) as file_paths:
self.data = json.load(file_paths)
self.font_file = os.path.join(self.font_dir, self.data["fonts"][1])
self.background_img = pg.image.load(os.path.join(self.img_dir, self.data["images"]["menu_imgs"]["background"]))
self.logo_img = pg.image.load(os.path.join(self.img_dir, self.data["images"]["icon"]))
self.height, self.width = 1000, 600
self.screen = pg.display.set_mode((self.height, self.width))
pg.display.set_caption("Battle on Tower {MENU}")
pg.display.set_icon(self.logo_img)
self.colors = [(0, 0, 0), (255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255)]
self.menu = True
self.menu_cycle()
def menu_cycle(self):
while self.menu:
for event in pg.event.get():
if event.type == pg.QUIT:
self.menu = False
self.button_press_event(event)
self.update_screen()
def text_render(self, text, font, size, color, x, y):
font = pg.font.Font(font, size)
text = font.render(text, True, color)
self.screen.blit(text, (x, y))
def draw_buttons(self):
global play_btn, quit_btn, about_btn
play_btn = pg.draw.rect(self.screen, self.colors[-2], (400, 200, 200, 75))
quit_btn = pg.draw.rect(self.screen, self.colors[2], (400, 300, 200, 75))
about_btn = pg.draw.rect(self.screen, self.colors[1], (400, 400, 200, 75))
self.text_render("Play", self.font_file, 60, self.colors[0], 420, 210)
self.text_render("Quit", self.font_file, 60, self.colors[0], 420, 310)
self.text_render("About", self.font_file, 50, self.colors[0], 400, 420)
pg.draw.rect(self.screen, self.colors[-1], (400, 200, 200, 75), 3)
pg.draw.rect(self.screen, self.colors[-1], (400, 300, 200, 75), 3)
pg.draw.rect(self.screen, self.colors[-1], (400, 400, 200, 75), 3)
def button_press_event(self, event):
global play_btn, quit_btn, about_btn
if event.type == pg.MOUSEBUTTONUP:
mouse_pos = pg.mouse.get_pos()
if play_btn.collidepoint(mouse_pos):
App()
self.menu = False
if quit_btn.collidepoint(mouse_pos):
self.menu = False
if about_btn.collidepoint(mouse_pos):
About()
self.menu = False
def update_screen(self):
self.screen.blit(self.background_img, (0, 0))
self.draw_buttons()
pg.display.update()
if __name__ == "__main__":
menu = Menu()
Class method, where are creating intances of classes from other scripts:
def button_press_event(self, event):
global play_btn, quit_btn, about_btn
if event.type == pg.MOUSEBUTTONUP:
mouse_pos = pg.mouse.get_pos()
if play_btn.collidepoint(mouse_pos):
App()
self.menu = False
if quit_btn.collidepoint(mouse_pos):
self.menu = False
if about_btn.collidepoint(mouse_pos):
About()
self.menu = False
Photo of the error enter image description here
Link to my GitHub with full version of my project
pyinstaller command:
pyinstaller --windowed --onefile --add-data "data;." --add-data "about.py;." --add-data "code.py;." --add-data "file_paths.json;." --icon=icon.ico menu.py
Solution 1:[1]
It means that "App" is not a Part of "code" Could be wrong caps or name, Or you don't have a "code" library, or an issue with the code of the Library.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wyatt Korth |
