I have the following directory structure:
.
|
|--- __init__.py
|--- main.py
|--- FolderA
| |
| |--- __init__.py
| |--- scriptA.py
| |--- config.py
|--- FolderB
|
|--- __init__.py
|--- scriptB.py
|--- config.py
scriptA.py:
import config
print(config.something)
scriptB.py:
import config
print(config.something)
config.py (FolderA):
something = "A"
config.py (FolderB):
something = "B"
scriptA.py
should import the config.py
from FolderA
, and scriptB.py
the config.py
from FolderB
.
Now, in main.py
I would like to directly import both scripts and both configs. All .py
files should be executed directly in their respective location, i.e. scriptA
should run in FolderA
, and main.py
in .
main.py:
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), 'FolderA'))
sys.path.append(os.path.join(os.path.dirname(__file__), 'FolderB'))
from FolderA import scriptA #runs scriptA
from FolderB import scriptB #runs scriptB
Output:
"A"
"A" #should be "B"
For some reason, the config from FolderB is not used in scriptB
I have read this resource, which tells me that this is not directly possible in python3. However, is there a reasonable workaround so that I can fully import and use all scripts and configs in main.py
(e.g. import FolderA.config as configA
) and ensure that the scripts are executable in their respective folders as well?
Edit:
I have added a working main.py that clearly shows the problem.