0

Pyinstaller 3.4 Python 3.7 (Windows 7)

I have a use-case where my main python script, foo.py, generates a traylist.py file that I want to import stuff from. foo.py occasionally has to update this traylist.py file. Therefore, there is a need for foo.py to be able to import that traylist.py again in a function. I accomplish this using a reload module function like this:

import traylist importlib.reload(traylist) menu_def = traylist.menu_def

I have also tried the following code with the same results (works within interpreter but fails when run as .exe)

import traylist del sys.modules['traylist'] import traylist menu_def = traylist.menu_def

All this works when running foo.py within the normal interpreter (python foo.py). However, when I create a --onefile .exe using pyinstaller, the values in the traylist module never refresh. I've checked the paths using sys._MEIPASS and can see the new file generated in the MEIxxx folder. I just can't get the .exe to reload that module correctly.

Frak
  • 832
  • 1
  • 11
  • 32

1 Answers1

0

It turns out that while pyinstaller lets you bundle other types of files and access them through the sys._MEIPASS path, that does not hold true for import module statements. It has a separate way that it handles those kinds of files and can't be changed easily resulting in stale copies of the same original module getting reloaded.

The solution was to load a module by providing its full path. Their are various methods to accomplish this but the one that worked for me was the one in this answer. First I had to do a pip install import_file, then: from import_file import import_file traylist = import_file(traylist_path)

Frak
  • 832
  • 1
  • 11
  • 32