Reputation: 85
I have what it seems to be an unusual situation.
── folder1
├── run.py
└── pgm.py
── folder2
└── src
├── fileA.py
└── fileB.py
── folder3
└── src
├── file1.py
└── file2.py
Folder1 is mine, folders 2 and 3 are in forked and I don't want to change it, and I'm not sure I can put the three of them in a over-Folder.
In run.py, I have:
sys.path.append(path_folder2)
from src.fileA import classA
That ClassA, call pgm.py thanks to:
module = importlib.import_module('pgm')
And pgm tries to import file1.py in folder3.src which import himself only files of folder3. Adding folder3 to path raise an ImportError. I presume it's because python looks in the subfolder src of folder2, how can I force to look in folder3 ?
In fact I'd like to be in the folder2 environnment as if folder 3 doesn't exist and then in folder3 as if folder2 doesn't exist during the same execution. How can I do that ?
Note: I didn't wrote the init.py for readability but they are in my folders and subfolders I tried to play with path, removing folder2 just before importing file1.py but no success.
Upvotes: 2
Views: 3162
Reputation: 106
In the newer versions of Python you can skip using the __init__.py (lookup feature called "implicit namespace package" for more details). There are some downsides to doing that, but I believe removing the __init__.py from the src directories will resolve your error.
Upvotes: 0
Reputation: 9097
You can rename module on import, like this:
from src.fileA import classA as fileAclassA
from src.fileB import classA as fileBclassA
Upvotes: 1