I've built a project with Python in which a module of functions can be changed by the user. More specifically, functions can be added or deleted inside this module by other processes in the application. Now I have just converted the whole project into an executable file using auto-py-to-exe to run it through a console window, instead of running it through VS Code for instance. I can't change the module if it was not added as an additional file in auto-py-to-exe rather can the application use this module if I do add it as an additional file. My question is: how can I turn this project into an executable with the possibility of changing this module by the program itself?
-
1An executable which tries to modify itself will either fail or it should alarm every virus scanner. – Michael Butscher Dec 16 '21 at 00:50
-
Please provide enough code so others can better understand or reproduce the problem. – Community Dec 22 '21 at 18:54
1 Answers
This may be tricky, but it is possible with tools like pyinstaller
when you bundle your app as a directory, rather than a single file executable. The source files (albeit compiled) will be present in the directory.
In principle, you could edit files in a single-file executable, but it's probably more trouble than it's worth and may be blocked by permissions and other issues.
You could also design your software to read a file from a predefined location (or the bundle directory -- anywhere accessible by the user) and simply exec
the string of the code to 'load' it. It could look more or less like an extension/scripting system for your program. One example comes to mind: the iterm2 software Python API.
It should go without saying that you must trust your users/inputs if you give them the ability to arbitrarily change the software code.

- 29,298
- 3
- 45
- 86
-
Thank you so much! Could you however give me an example of exec the string of the code? I will give you some further details: say I have a main file which has a module that receives code input as text and alocates it inside functions in another module. Afterware the main script will use this module with functions to run each one of them one by one. Running through VS Code already works, because it just imports the module that was just modified. How could I "import" or "load" it then inside the executable? Could you give some code example please? – Matheus Janczkowski Dec 16 '21 at 01:05
-
If this module was to be saved in a text format rather than .py and, for each function, the executable select the piece of text containing it and run through the process described on the following link https://stackoverflow.com/questions/701802/how-do-i-execute-a-string-containing-python-code-in-python would it be a good solution? – Matheus Janczkowski Dec 16 '21 at 01:23
-
The extension of the file doesn't matter - both would _work_ with the solution described in the question you linked @MatheusJanczkowski -- whether this is a _good_ solution is a matter of opinion and contextual to the purpose of your application. – sytech Dec 16 '21 at 01:24
-
Let's say I as developer upgrade this module in a database, how could the executable access this module and run instead of executing strings? – Matheus Janczkowski Dec 16 '21 at 09:43