For a project that I’m working on, I want in my distributable version to have certain Python scripts be bundled not as part of the executable, but rather as external resources. (Such that one can potentially drop new scripts into position after installation on a user’s machine–e.g. for DLC.)
Now, if I simply have a flat file-hierarchy, with all scripts in the same directory, this seems to work as expected.
However, things get complicated when I want to keep my “external scripts” in a sub-directory: the executable seems to no longer find them, even when they’re present.
By way of demonstration I’ve attached below a zip-file containing two versions of the same mini-program–one in a directory named “Raw” and one in a directory named “Frozen”. The former has the program simply as a collection of Python files, while the latter has the same, but with a “setup.py” and “requirements.txt” with which to build a distributable.
ImportTester.zip (4.0 KB)
(I tried just providing pre-built distributables of the “Frozen” version, but hit the maximum file-size for uploads to the forum.)
To test:
- Open the “Raw” folder and run the following (or equivalent for your system):
python3 core.py
- Note the output produced
- Open the “Frozen” folder
- Within that, open the “setup.py” file and modify the “platforms” section to suit your OS
- Build a distributable by running the following (or, again, the equivalent for your system):
python3 setup.py bdist_apps
- Open the new “dist” folder and extract the zip file within to a convenient location
- Go to that location and open the extracted folder
- Navigate to the executable (which should be named “Import Tester”) and run it in a console/terminal
- Note the output produced
The program is simple: it imports two sub-scripts, one via a standard “import”-statement, that one being bundled with the executable, and another dynamically via “importlib”, that other being kept external to the executable.
It should then print out the following, one line coming from each script:
I am a Kitten
I am a Moggy
On my machine, at least, the “Raw” version works as expected, producing the above output.
However, the “Frozen” version doesn’t: it produces the following output:
I am a Kitten
Traceback (most recent call last):
File "__main__", line 9, in <module>
File "importlib", line 126, in import_module
File "importlib._bootstrap", line 994, in _gcd_import
File "importlib._bootstrap", line 971, in _find_and_load
File "importlib._bootstrap", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'SubClasses.secondary_dynamic'
Note, however, that the relevant script-file is included with the “Frozen” version, along with an “__init__.py” file.
So, does anyone know where I’m going wrong, and/or how to fix this…?