[Python-Dev] Issues about relative& absolute import way for Portingfrom python2.4 to python2.7

Peter Otten __peter__ at web.de
Thu Mar 20 08:21:03 CET 2014


北冰洋 wrote:

> I just wrote a sample like this:
> testPy/
>          __init__.py
>          client.py
>          SoamFactory.c
>          SoamFactory.so
>          soamapi.py
>          sample/testP.py
> export PYTHONPATH=$(TEST_LOCATION):$(TEST_LOCATION)/testPy

> I found that soamapi was imported twice, and I investigated this is

> I don't know how to fix it. Is there any official way about how to porting
> this scenario or better idea?

Without looking into the details -- I believe that your problem stems from 
having a path into the package:

> export PYTHONPATH=$(TEST_LOCATION):$(TEST_LOCATION)/testPy

The above means that a module testPy.foo can also successfully be imported 
as foo. Try changing the path to

export PYTHONPATH=$(TEST_LOCATION)

If you get failing imports change every failing import

import foo

to

from testPy import foo




More information about the Python-list mailing list