Hi,
I simply don't know how to create a search string for this possibly super simple question...
I want to create a main.py which consists of several expressions, which directly execute
other *.py files (named in the main.py) in sequence with the same effect, as if their code
would be included in the main.py.
Beside the name of the files themselves there should be a less information about the contents
of those files in the main.py as possible...
Is this even possible and if yes...how?
Cheers!
mcc
How to exec py-file as a main.py replacement?
-
- Posts: 67
- Joined: Fri Sep 04, 2020 9:27 am
- Location: Hanko, Finland
- Contact:
Re: How to exec py-file as a main.py replacement?
Perhaps this thread helps viewtopic.php?t=6179
Re: How to exec py-file as a main.py replacement?
In Python, when you "import foo" it executes the contents of foo.py
If you want to "re-import" it, you can "del" it from sys.modules first.
So if I have foo.py:
then in main.py I can run
to run the contents of foo.py five times. Or if I just wanted to run a few different modules once each, I could just import them one after the other.
If you want to "re-import" it, you can "del" it from sys.modules first.
So if I have foo.py:
Code: Select all
print("hello")
Code: Select all
for i in range(5):
import foo
del sys.modules['foo']
Re: How to exec py-file as a main.py replacement?
Alternative to the above, less 'magic':
foo.py:
main.py:
foo.py:
Code: Select all
def Foo():
print("hello")
Code: Select all
import foo
for i in range(5):
foo.Foo()