Don't understand module search path...

Discussion in 'Python' started by mhearne808[insert-at-sign-here]gmail[insert-dot-he, Oct 4, 2007.

  1. I think I don't understand how the module search path works...

    Let's say I have a folders called 'test'. Underneath it, I create two
    more folders called 'foo' and 'bar'.

    In 'foo', I create an empty '__init__.py' file, indicating that this
    folder is a package 'foo'. I then create a simple python script
    'foo.py' consisting of the following code:

    ----------------------------
    #!/usr/bin/python

    def printhello():
    print 'Hello world!'
    ----------------------------

    Then in test/bar, I create 'bar.py' consisting of the following code:
    ----------------------------
    #!/usr/bin/python
    import sys
    import os
    (curpath,thisdir) = os.path.split(os.getcwd())
    foopath = os.path.join(curpath,'foo')
    sys.path.append(foopath)
    print sys.path
    os.chdir(os.path.join(os.getcwd(),'..'))
    print os.getcwd()
    from foo.foo import printhello
    ----------------------------

    When I try to run bar.py, I get the following:

    ----------------------------
    [sys.path search path, including full path to 'foo' folder]
    path/to/test
    Traceback (most recent call last):
    File "/path/to/test/bar/testfoo.py", line 16, in <module>
    from foo.foo import printhello
    ImportError: No module named foo
    ----------------------------

    Why? If 'foo' is in sys.path, shouldn't it appear when I try to
    import the foo module from it? Incidentally, when I move the script
    up to 'test' and modify it so that it just says:
    ----------------------------
    #!/usr/bin/python

    from foo.foo import printhello
    ----------------------------

    I get no errors. I don't understand the difference...

    Incidentally, my platform info:
    Python 2.5.1
    Darwin Kernel Version 8.10.1 (Mac OS X)

    Help!

    --Mike
     
    mhearne808[insert-at-sign-here]gmail[insert-dot-he, Oct 4, 2007
    #1
    1. Advertising

  2. mhearne808[insert-at-sign-here]gmail[insert-dot-he

    Chris Mellon Guest

    On 10/4/07, mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
    <> wrote:
    > I think I don't understand how the module search path works...
    >
    > Let's say I have a folders called 'test'. Underneath it, I create two
    > more folders called 'foo' and 'bar'.
    >
    > In 'foo', I create an empty '__init__.py' file, indicating that this
    > folder is a package 'foo'. I then create a simple python script
    > 'foo.py' consisting of the following code:
    >
    > ----------------------------
    > #!/usr/bin/python
    >
    > def printhello():
    > print 'Hello world!'
    > ----------------------------
    >
    > Then in test/bar, I create 'bar.py' consisting of the following code:
    > ----------------------------
    > #!/usr/bin/python
    > import sys
    > import os
    > (curpath,thisdir) = os.path.split(os.getcwd())
    > foopath = os.path.join(curpath,'foo')
    > sys.path.append(foopath)
    > print sys.path
    > os.chdir(os.path.join(os.getcwd(),'..'))
    > print os.getcwd()
    > from foo.foo import printhello
    > ----------------------------
    >
    > When I try to run bar.py, I get the following:
    >
    > ----------------------------
    > [sys.path search path, including full path to 'foo' folder]
    > path/to/test
    > Traceback (most recent call last):
    > File "/path/to/test/bar/testfoo.py", line 16, in <module>
    > from foo.foo import printhello
    > ImportError: No module named foo
    > ----------------------------
    >
    > Why? If 'foo' is in sys.path, shouldn't it appear when I try to
    > import the foo module from it?


    No. foo will be searched for modules, but foo itself won't be found
    (because it's looking *inside* foo). You want "test" to be on sys.path
    for this to work.

    >Incidentally, when I move the script
    > up to 'test' and modify it so that it just says:
    > ----------------------------
    > #!/usr/bin/python
    >
    > from foo.foo import printhello
    > ----------------------------
    >
    > I get no errors. I don't understand the difference...
    >


    The directory that the executing script is in is implicitly on
    sys.path, so when you do this you place "test" in sys.path, and foo is
    found.
     
    Chris Mellon, Oct 4, 2007
    #2
    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. simon

    Error message I don't understand

    simon, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    319
    Hatim R
    Dec 5, 2003
  2. m miller

    don't understand error

    m miller, Jan 9, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,744
    smitdso
    Sep 8, 2006
  3. Timmy
    Replies:
    5
    Views:
    485
  4. Stéphane Klein
    Replies:
    0
    Views:
    290
    Stéphane Klein
    Jul 5, 2011
  5. Abby Lee
    Replies:
    5
    Views:
    443
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page