Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Difference between import in script and from interpreter

Reply
Thread Tools

Difference between import in script and from interpreter

 
 
Edward Diener
Guest
Posts: n/a
 
      07-19-2010
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.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-19-2010
On Mon, 19 Jul 2010 00:53:56 -0400, 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.


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


--
Steven
 
Reply With Quote
 
 
 
 
News123
Guest
Posts: n/a
 
      07-19-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
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      07-19-2010
Steven D'Aprano wrote:
> On Mon, 19 Jul 2010 00:53:56 -0400, 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.
>>

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Surprising difference in behavior between "import blah" and "fromblah import thing" Eric Hanchrow Python 1 05-09-2008 05:54 AM
Re: Surprising difference in behavior between "import blah" and"from blah import thing" J. Cliff Dyer Python 2 05-08-2008 09:17 PM
Import - interpreter works but .py import does not John Boy Python 1 03-10-2008 04:07 AM
Python embedded interpreter: how to initialize the interpreter ? ycollet@freesurf.fr Python 3 01-03-2007 01:00 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57