After some experiments I’ve found a solution.
-
Create a separate folder
source
in the main folder of the application. Here will be placed source files. Also place file__init__.py
to the folder. Lets name a main file likemain_module.py
. -
Add all of its contents as a data files to the py2exe configuration
setup.py
. Now after compiling the program, these files will be placed in the dist folder.data_files += [('source', glob('source/*.py'),)] setup( data_files=data_files, .... # other options windows=[ { "script": "launcher.py", "icon_resources": [(0, "resources/favicon.ico")] } )
-
Make
launcher.py
– it’s task is to import all system and required libraries like pygame, pyqt and so on. Then run you program:import sys, time, os, hashlib, atexit # std modules import PyQt5, ... # foreign libraries sys.path.insert(0, 'source') exec('import main_module')
-
Now
main_module.py
will be imported, if it imports your modules, they will be imported too in their places in hierarchy. For example head of themain_module.py
can be like this:import user_tweaks from user_data import parser
These files
user_tweaks.py
anduser_data.py
should be located insource
folder at appropriate paths relative tomain_module.py
.
You may change contents of source
folder without recompilation program itself. Any time program runs it uses fresh contents of source
.
As a result you have an application folder with:
- A separate launcher – simple .exe file
- All required modules
- Your application with all its modules.