Can I import a self-generated python script on runtime? #6655
-
My layout is like this: main.py - A script that based on some things generates python code and puts it in a .py file What I want is to run the code within that internal_code.py.
The same happens when I use Is what I'm trying possible? The closest question I found was if it is possible to run static python code by importing it, which works, at least with some extra stuff while using pyinstaller like "paths". But that is not an option if I generate my code on the fly. And I know what some of you might think: "Why can't you just code logic into main.py that does the same as the code you generate? That should be even easier than doing it your way." Any ideas and answers are appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I seen so many people come to this conclusion at some point and every time they've been wrong so I'm afraid that I don't believe you. When is this code generated? Before you run PyInstaller or at runtime? PyInstaller won't stop you doing either but if it's at runtime then you'll have to be careful where you write that generated file to because there's a good chance that the rest of your code will be installed into a directory requiring privileged access to write to. The rules for importing files are pretty straight forward and are identical for PyInstaller as they are for non-PyInstaller: If a module is in a directory and that directory is in |
Beta Was this translation helpful? Give feedback.
-
you can use Pyinstaller and "importlib.import_module" to run your script at run time. I made a sample demo at https://github.com/RandySabatin/PyScriptRunner |
Beta Was this translation helpful? Give feedback.
I seen so many people come to this conclusion at some point and every time they've been wrong so I'm afraid that I don't believe you.
When is this code generated? Before you run PyInstaller or at runtime? PyInstaller won't stop you doing either but if it's at runtime then you'll have to be careful where you write that generated file to because there's a good chance that the rest of your code will be installed into a directory requiring privileged access to write to.
The rules for importing files are pretty straight forward and are ident…