Difference between import in script and from interpreter

E

Edward Diener

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.
 
S

Steven D'Aprano

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.

And how is sys.path different when you run it as a script?
 
N

News123

Edward said:
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
 
J

Jean-Michel Pichavant

Steven said:
And how is sys.path different when you run it as a script?
'' is not in sys.path when running a script.
'' is in sys.path by default within a interpreter.


JM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top