Import function works in IDLE but not as an exe

Viewed 36

I'm trying to import data from .py files, but when I run my game as an exe (I used pyinstaller to make my game into an exe), It dosen't work. I dont get an error or anything, it just dosent import the data. The weird thing is that when running this from IDLE, it works perfectly fine.

def achivementcheck():
    f = open("simplesavedata.py", "w")
    f.write("achivements = " + str(achivements) + "\n")
    f.close()

def option_save():
    f = open("optiondata.py", "w")
    f.write("volume = " + str(volume))
    f.close

from simplesavedata import *
from optiondata import *

EDIT: My game code

import random
import pickle
import os
from just_playback import Playback
import sys
playback = Playback()


init(convert=True)


volume = 0
achivements = 0
achivename = 0


def achivementcheck():
    f = open("simplesavedata.py", "w")
    f.write("achivements = " + str(achivements) + "\n")
    f.write("achivename = " + str(achivename) + "\n")
    f.close()

def option_save():
    f = open("optiondata.py", "w")
    f.write("volume = " + str(volume))
    f.close


#load saved data
from simplesavedata import *
from optiondata import *


playback.load_file("music/titlescreen.mp3")
playback.play()
playback.loop_at_end(True)

name = input("What is your name? ")

if name == "test":
     print("Test achivement")
     achivements += 1
     achivename += 1
     achivementcheck():

print("achivements = ", achivements)
print(gotten achivename", achivename, "times")
1 Answers

Using the code below and using pyinstaller -F filename.py the executable seems to work perfectly. Make sure you are actually calling your functions so the code executes before trying to import the newly created python files.

optiondata.json

{
    "achievement": 0,
    "volume": 0
}
import json

def get_achievements():
    with open("optiondata.json", "rt") as jsonfile:
        data = json.load(jsonfile)
        achievements = data["achievements"]
    return achievements

def save_achievements(value):
    with open("optiondata.json", "rt") as jsonfile:
        data = json.load(jsonfile)
    with open("optiodata.json", "wt") as jsonfile:
        data["achievements"] = value
        json.dump(jsonfile)

def save_volume(value):
    with open("optiondata.json", "rt") as jsonfile:
        data = json.load(jsonfile)
    with open("optiondata.json", "wt") as jsonfile:
        data["volume"] = value
        json.dump(jsonfile)

def get_volume():
    with open("optiondata.json", "rt") as jsonfile:
        data = json.load(jsonfile)
    return data['volume']
Related