0

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.

mto_19
  • 135
  • 6
  • 1
    So is `main.py` just responsible for launching `scriptA.py` and `scriptB.py`? You could always defer your imports and do something a bit more dynamic, e.g. `for group in ['A', 'B']: os.chdir(os.path.join(root, f'Folder{group}')); importlib.import_module(f'script{group}'); ...` – 0x5453 Aug 20 '19 at 12:38
  • IMO it would be far simpler to just rename `FolderA/config.py` to `configA.py` and similarly for `FolderB/config.py`. Would you really want to maintain such complicated imports 6 months down the line? – rdas Aug 20 '19 at 12:39
  • @rdas Of course I could rename the files, but the point of the question was if this can be achieved without renaming them. – mto_19 Aug 20 '19 at 12:42
  • @mtoller And my point was that even if it can, it's probably not worth doing it. – rdas Aug 20 '19 at 12:43
  • @0x5453 importlib seems like a good option, but your solution gives ` File "/./FolderA/scriptA.py", line 1, in from config import config ImportError: cannot import name 'config' from 'config' ` – mto_19 Aug 20 '19 at 12:45

2 Answers2

1

I solved the problem using imp.load_source as suggested in this answer:

main.py:

from imp import load_source

config=load_source('config','/home/mtoller/mre/FolderA/config.py')
from FolderA import scriptA

config=load_source('config','/home/mtoller/mre/FolderB/config.py')
from FolderB import scriptB

output:

 "A"
 "B"
mto_19
  • 135
  • 6
0

I don't know if I missunderstood something but it should work with:

import FolderA
import FolderB

print(FolderA.scriptA.config.something)

Or you could execute a funtion called foo from scriptA.py: FolderA.scriptA.foo()

Rafael-WO
  • 1,434
  • 14
  • 24