Home > other >  Pyinstaller not working when importing local functions and variables from another file
Pyinstaller not working when importing local functions and variables from another file

Time:02-27

Title. I have 3 files. 1 main file, to run the script. 1 function file, that holds the functions that the script uses. And 1 data file, that holds the variables that the script accesses. When I use pyinstaller to compile the main function to an exe, despite many attempts with a bunch of different options, (--onefile, --add-data, etc.) I haven't been able to figure out how to fix it. More detailed view on my setup:

# data file
my_bool = False
# functions file
def my_func():
    from data_file import my_bool
    my_bool = not my_bool
# main script
from data_file import my_bool
from functions_file import my_func
print(my_bool)
my_func()
print(my_bool)

For me, this actually does what it's supposed to do perfectly fine before the files are attempted to be compiled. Of course this isn't my actual setup but it does work like I expected after checking it, outputting:

False
True

So after all that explanation, I'm just wondering how I can fix this issue. I assume it has something to do with how I import everything, but how should I fix that? I always could, but I don't want to be forced to rewrite this whole project over one small, probably fixable issue. Thanks in advance

CodePudding user response:

First of all, Pyinstaller is very Finicky. Most newer versions are not compatible with some modules, i.e. the latest Pyinstaller will not work with Pygame Fonts, at least in my experience, so consider checking the log files, or error messages it creates.

Secondly, for Pyinstaller to work with multiple files, they all need to be in the same directory, even the same subdirectory. TechwithTim has a great video explaining all of this, but the long and short of it is, that you need to have all your files and dependencies on the same level, and with the same access level.

Other than that, I cannot see anything wrong with it per see. Not only has the code expert look pretty simple, but I have managed to do heavier stuff with no issue, which makes me think it's probably a compatibility issue with the Modules your using and Pyinstaller.

Hope this helps, and I do really recommend that video I mentioned earlier.

  • Related