views:

50

answers:

1

I have a project with a directory structure that looks like:

/foo/baz/__init__.py
/bar/foo.py
/bar/splat.py

Problem is, /bar/splat.py refers to the foo.baz module. This fails with the error No module named baz because it's trying to search for this module within /bar/foo.py. I don't want Python to search the bar module, I want to tell it to search the root foo module for baz. How do I do that? In Ruby you'd just prefix the identifier with :: (In this case, ::Foo::Baz), is there a Python equivalent to this?

+4  A: 

In Python 2.5 and 2.6,

from __future__ import absolute_import

should change Python's import behavior to do what you want (if the very root, /, is on sys.path of course;-). This becomes the normal Python behavior in 2.7 (not released yet, but an early alpha is already tagged, if you're curious).

Alex Martelli
Cool, that worked, thanks.
Bob Aman
Incidentally, this is an App Engine project, so it's Python 2.5.2.
Bob Aman
@Bob, always glad to help -- btw, mentioning the Python release(s) of interest in the question's a good idea, in general, since some environments (e.g. GAE) constrain you to 2.5, in most cases people can use 2.6, others are trying 3.1, etc, and the best answers may often depend on what version(s) you care about!-)
Alex Martelli