简体   繁体   中英

import python module using sys.path.append

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

I'm afraid you can't do it that way.

Because of structure:

/home/sam/pythonModules/module1/lib/module1.py
/home/sam/pythonModules/module2/lib/module2.py

You can't put both:

  • /home/sam/pythonModules/module1 and
  • /home/sam/pythonModules/module2

in sys.path and expect that Python will find:

  • module1 in module1/lib and
  • module2 in module2/lib

when you try import like:

from lib.module1 import A
from lib.module2 import B

If you put /home/sam/pythonModules/module1 before /home/sam/pythonModules/module2 in sys.path array, then import lib.MODULE will search for MODULE in /home/sam/pythonModules/module1/lib .

Since there is only module1 and no module2 in it, you get error.

What you can do is to put both

  • /home/sam/pythonModules/module1/lib/ and
  • /home/sam/pythonModules/module2/lib/

in sys.path and expect Python to correctly import them with next lines:

from module1 import A
from module2 import B

Useless to use sys.path.insert , only if you want to prioritize your project before another in PYTHONPATH .

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 

Your projects module1 / module2 should be also structured as valid package, let see official guidelines: http://docs.python-guide.org/en/latest/writing/structure/

I am not sure why you choose to use sys.insert for your case.

sys.path.insert(1, "/home/sam/pythonModules/module1")

this will overwrite the second element in your search list. I think this is the current folder in my case (in pyCharm). Do you want this?

sys.path.append("/home/sam/pythonModules/module2")

add to the current search list so it does not overwrite anything. But it will be the last place to search (after searching all previous ones that is). If you have any file with the same name for example (not a good idea anyway) it will be executed first.

from lib.module1 import A

you are importing from lib folder but where is this folder? From your structure it does not appear.

You should be able to import both using just:

from module1 import A
from module2 import B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM