1

I am creating a Python 3.9 game using Pygame. I want to bundle it as a .app file using py2app, but pygame needs Absolute Paths for the assets I'm using. I want to use PathLib to find the base directory, but the code I have results in the FileNotFoundError: No such file or directory. error.

Here is a sample of my code:

import os.path
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
ASSETS_DIR = os.path.join(BASE_DIR, 'assets')

# Load images
RED_SPACE_SHIP = pygame.image.load(os.path.join(ASSETS_DIR, 'pixel_ship_red_small.png'))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join(ASSETS_DIR, 'pixel_ship_green_small.png'))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join(ASSETS_DIR, 'pixel_ship_blue_small.png'))

Please can someone tell me what my issue is?

DillonB07
  • 154
  • 11
  • Thanks, but I still got the same `FileNotFoundError: No such file or directory.` error. Should I have changes my usages of BASE_DIR and ASSETS_DIR as well? – DillonB07 Jul 06 '21 at 14:52
  • Printing `os.chdir(os.path.dirname(os.path.abspath(__file__)))` returns `None` as well.... – DillonB07 Jul 06 '21 at 14:57

1 Answers1

1

I used an extra .parent

That was the issue so it was looking in the wrong directory.

DillonB07
  • 154
  • 11
  • Dear DillonB07, please remove SOLVED! and the emoji, it's an answer, explain completely what was your fault and how you fix it. I upvoted your answer, but please edit it. – AmerllicA Feb 07 '22 at 18:16