Reputation: 223
I have added another python project to my project as a git-submodule to reuse it's functionality. Now in my project i am able to use the libraries with absolute path.
But inside those libraries, import statements are are failing now.
All libraries have:
from automation.corelib.BaseApi import BaseAPI
when i change it to absolute path
from Systemtest.Services.automation.corelib.BaseApi import BaseAPI
which works, but that cannot be done, since it will deviate from the sub-module branch and there code can not be changed
Is there any work-around where while automation.corelib.BaseApi
it should do a relative import, and no file change in 3rd party lib. is required.
Version: python3.6
This is the structure:
----project--- (master branch)
|
------submodule1 (dev. branch)
|
------submodule2 (dev. branch)
|
------Testcase (accessing submodule 1 & 2)
There are folders with same name automation.corelib
in both submodule 1 & 2, which is not getting resolved. Both sub-module are added to sys.path, but only 1 works, if file not found in automation folder in submodule 1, it do not check in automation folder of submodule 2
With absolute name & proper hierarchy it works. But within the submodule it fails, and there path also cant be modified, since that submodule is used by other people
HERE IS THE DUMMY PROJECT TO VISULAIZE THE PROBLEM
There are 2 import statements, in sample_test.py:
from automation import dummy_module1
from automation import dummy_module2
one automation folder is in submodule1, another is in submodule2. Path for both is added in sys.path. But only one gets resolved.
i am module1
Traceback (most recent call last):
File "C:/Users/Test/PycharmProjects/TestImport/test/sample_test.py", line 2, in <module>
from automation import dummy_module2
ImportError: cannot import name 'dummy_module2'
Process finished with exit code 1
I got that it found automation folder is submodule1 so first got passed but in second statement it didn't tried in submodule2. Is there any way to fix this, without modifying the import statement (as it is part of some other project too).
On giving parent directory in import statement it works, but not allowed in my scenario
I tried with modifying the sys.path in sample_test.py at the run time, but that too is not working. Something like:
import sys
sys.path.append('C:\\Users\\Test\\PycharmProjects\\TestImport\\submodule1')
from automation import dummy_module1
sys.path.remove('C:\\Users\\Test\\PycharmProjects\\TestImport\\submodule1')
sys.path.append('C:\\Users\\Test\\PycharmProjects\\TestImport\\submodule2')
from automation import dummy_module2
Upvotes: 1
Views: 259
Reputation: 356
When you use sys.path.append('C:\\Users\\Test\\PycharmProjects\\TestImport\\submodule1')
you are telling the python import system to search only files within the submodule1
. This means that the submodule1
is not recgonized as a module by itself. This may work, but also may lead to errors in the future depending on how the submodule1
changes in the long run.
Your current issue may be fixed by doing something like this:
import sys
# Tells Python to look for `automation` under the directory `submodule1`,
# thus making the `from automation import dummy_module1` to work
sys.path.append('C:\\Users\\Test\\PycharmProjects\\TestImport\\submodule1')
# Tells Python to look for `submodule2` under the directory `TestImport`,
# thus making `from submodule2.automation import dummy_module2` to work.
sys.path.append('C:\\Users\\Test\\PycharmProjects\\TestImport\\')
from automation import dummy_module1
from submodule2.automation import dummy_module2
Note that when using sys.path.append('C:\\Users\\Test\\PycharmProjects\\TestImport\\')
Python will recognize both submodule1
and submodule2
as modules despite the fact that it also recognized files under submodule1
separately in the previous statement. I'm not sure about he kind of issue that this may lead to.
I suggest you to consider a code refactoring in order to avoid this type of work around. Namespaces are a great feature used to exactly prevent this kind of issue.
Upvotes: 0