467,893 Members | 1,916 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,893 developers. It's quick & easy.

Don't understand module search path...

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

Oct 4 '07 #1
  • viewed: 1284
Share:
1 Reply
On 10/4/07, mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
<mh********@gmail.comwrote:
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.
Oct 4 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stephen Ferg | last post: by
6 posts views Thread by kimes | last post: by
1 post views Thread by aznach | last post: by
17 posts views Thread by =?Utf-8?B?Y2F0aGFyaW51cyB2YW4gZGVyIHdlcmY=?= | last post: by
6 posts views Thread by Theo v. Werkhoven | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.