problem with import / namespace

Laszlo Nagy gandalf at shopzeus.com
Wed May 21 10:35:11 EDT 2008


>
> When you try to import a module, python starts to search for it. The 
> was it does the search is very well defined. It mostly depends on the 
> current directory and sys.path. You can read more about this here:
"The was it" -> "The way it"
> - inside your app.py file either make sure that the current dir is 
> /app, or insert /app in the first place in sys.path
Example:

import os,sys
mydir = os.split(os.abspath(__file__)[0]
os.chdir(mydir) # Option 1 - chdir to dir. containing your app
sys.path.insert(0,mydir) # Option 2 - add to your sys.path

Good things to know:

- In Python, it is recommended not to use upper case module/package 
names. It is a convention. However, some 3rd party packages (like 
wxPython or PIL) break this rule.
- Obviously, you should not use numbers, reserved words for module or 
package names. The name should tell what is it for.
- It is good to know the standard library, and avoid overwriting names 
from the standard lib. For example, you can create your own 'os.py' 
module but it would be very silly.

I personally tend to use absolute package names for libraries that are 
not tied to a specific project but it does not need to be that way.

You can find many threads in python-list about how applications and 
packages should be constructed.

Best,

   Laszlo




More information about the Python-list mailing list