I have two python modules which I am trying to import
using sys.path.append
and sys.path.insert
. Following is my code
import sys
sys.path.insert(1, "/home/sam/pythonModules/module1")
sys.path.append("/home/sam/pythonModules/module2")
from lib.module1 import A
from lib.module2 import B
I have following folder structure
/home/sam/pythonModules/module1/lib/module1.py
/home/sam/pythonModules/module2/lib/module2.py
I am able to import lib.module1 but not lib.module2. If I do it like this
import sys
sys.path.insert(1, "/home/sam/pythonModules/module2")
sys.path.append("/home/sam/pythonModules/module1")
from lib.module1 import A
from lib.module2 import B
then I am able to import module2
but not module1
.
What can be the reason for the above importing errors?
I tried append
instead of insert
in following manner but it's still doesn't work
import sys
sys.path.append("/home/sam/pythonModules/module1")
sys.path.append("/home/sam/pythonModules/module2")
from lib.module1 import A
from lib.module2 import B
Always only first module in sys.path.append
is successfully imported.
But I make some changes to paths in sys.path.append
in following manner then it works. Both the modules are imported successfully
import sys
sys.path.append("/home/sam/pythonModules/module1")
sys.path.append("/home/sam/pythonModules/module2/lib")
from lib.module1 import A
from module2 import B