Difference between import in script and from interpreter

Discussion in 'Python' started by Edward Diener, Jul 19, 2010.

  1. 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.
    Edward Diener, Jul 19, 2010
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Jul 19, 2010
    #2
    1. Advertising

  3. Edward Diener

    News123 Guest

    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
    News123, Jul 19, 2010
    #3
  4. 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
    Jean-Michel Pichavant, Jul 19, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    11,973
  2. Ulf Meinhardt
    Replies:
    1
    Views:
    4,549
    Tor Iver Wilhelmsen
    Oct 14, 2004
  3. John Boy
    Replies:
    1
    Views:
    300
    John Machin
    Mar 10, 2008
  4. Eric Hanchrow
    Replies:
    1
    Views:
    352
    Arnaud Delobelle
    May 9, 2008
  5. J. Cliff Dyer
    Replies:
    2
    Views:
    317
    Chuckk Hubbard
    May 8, 2008
Loading...

Share This Page