Difference between import in script and from interpreter

News123 news1234 at free.fr
Mon Jul 19 03:10:24 EDT 2010


Edward Diener wrote:
> In a python script a:
> 
> from xxx.yyy.zzz import aaa
> 
> fails with the message:
> 
> "ImportError: No module named xxx.yyy.zzz"
> 
> but from within the python interpreter the same line succeeds. What
> would be the causes of that ?
> 
> From within the python interpreter I have looked at sys.path and
> xxx.yyy.zzz is definitely in the path ( off of site-packages ). So I am
> not sure why this is failing within the python script.

Probably your problem is, that you call a python script, which is NOT
located in the current working directlory.


if you type python

then imports will be relative to your current working directory

if you execute a script imports will be relative to the scripts location.


no idea in which directory you are when starting python from a windows menu.

you can set
PYTHONPATH to point to the base directory of your project if you want to
be sure to always find your modules



More information about the Python-list mailing list