472,958 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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
1 1434
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephen Ferg | last post by:
I need a little help here. I'm developing some introductory material on Python for non-programmers. The first draft includes this statement. Is this correct? ...
6
by: kimes | last post by:
I've just started digging into how python works.. I found that other mudules are clearly declared like one file per a module.. But the only os.path doesn't have their own file.. ye I know is...
1
by: aznach | last post by:
Hello! I have a shared hosting account at GrokThis.net and have a problem with the module order of the Python search path. I'd like to use django's svn trunk instead of the...
17
by: =?Utf-8?B?Y2F0aGFyaW51cyB2YW4gZGVyIHdlcmY=?= | last post by:
Hello, I have build a website with approximately 30 html-pages. When I search this website in Google, I see the index.html or home.html on this website, but also other html-pages on this...
6
by: Theo v. Werkhoven | last post by:
Goodday, Something strange going on here. A piece of code I wrote bombs out in one of de directories under $HOME, but not in others. Here's a snipped: #v+ def bin2asc(c): s=''
4
by: John Nagle | last post by:
Python's own loader searches "sys.path" for module names, but is there some function that makes that search functionality accessible to Python programs? I need the absolute pathname of a module,...
0
by: js | last post by:
Hi list, Is it possible to change module search path (PYTHONPATH) built-in to Python interpreter? I thought I can change it with configure --libdir but it didn't work for me. I also tried...
2
by: Mike Driscoll | last post by:
On Sep 25, 10:41am, js <ebgs...@gmail.comwrote: Why not just add a custom path file (*.pth)? EasyInstall, wx, PyWin32 and others do it. Of course there's always sys.path.append as well. Mike
7
by: sara | last post by:
I have a friend doing some pro-bono work for a non-profit that does job training for distressed kids under DCSS care. He asked me for code to do the following (he's using A2003). I can't find...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.