I have a collection of scripts written in Python. Each of them can be executed independently. However, most of the time they should be executed one after the other, so there is a MainScript.py
which calls them in the appropriate order. Each script has some configurable variables (let's call them Root_Dir
, Data_Dir
and LinWinFlag
). If this collection of scripts is moved to a different computer, or different data needs to be processed, these variable values need to be changed. As there are many scripts this duplication is annoying and error-prone. I would like to group all configuration variables into a single file.
I tried making Config.py
which would contain them as per this thread, but import Config
produces ImportError: No module named Config
because they are not part of a package.
Then I tried relying on variable inheritance: define them once in MainScript.py
which calls all the others. This works, but I realized that each script would not be able to run on its own. To solve this, I tried adding useGlobal=True
in MainScript.py
and in other files:
if (useGlobal is None or useGlobal==False):
# define all variables
But this fails when scripts are run standalone: NameError: name 'useGlobal' is not defined
. The workaround is to define useGlobal and set it to False when running the scripts independently of MainScript.py
. It there a more elegant solution?