py2exe adding json files into library.zip or change script to load file from specified location

  json, py2exe, python

I’m trying to create a simple game with pygame and now that most of it has been tested, I wanted to package it into an executable to allow others to test.

Currently my issue is that I have json configuration files that the script is loading, this json files are configurations for levels, terrain, items, etc. It is loaded with the open function and then passed to the json.load function.

terrain_file = open(f'../terrain.json',)
terrain = json.load(terrain_file)

This code works when running the game through the command-line/terminal. However when I’m trying to create an executable with py2exe, this script seems to look for the file inside library.zip. My current py2exe setup is as follows:

from distutils.core import setup
import py2exe
import os
import glob, fnmatch

game_dir = os.path.dirname(os.path.realpath(__file__))

extra_files = [('.',['terrain.json','game.json'])]

setup(version='0.0.1', name='Simple Game', windows=[{'script':'game.py'}],data_files=extra_files, options={'py2exe':{'optimize':2,'skip_archive':True,'bundle_files':2}})

This copies my json files into the dist folder but the executable is still looking for the files inside library.zip

Any advice on how to either change the script so that it does not look for it inside library.zip or include the json files into library.zip

Source: Python Questions

LEAVE A COMMENT