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

  2. And how is sys.path different when you run it as a script?
    Steven D'Aprano, Jul 19, 2010
    1. Advertisements

  3. Edward Diener

    News123 Guest

    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
  4. '' is not in sys.path when running a script.
    '' is in sys.path by default within a interpreter.

    Jean-Michel Pichavant, Jul 19, 2010
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.