tags:

views:

288

answers:

1

I have now bumped into this problem twice recently and am curious if there is a solution.

So I have a module that confilcts with a python builtin. For example, say I have a myapp.email module defined in myapp/email.py.

Now anywhere in my code I can reference myapp.email just fine. However, I need to reference the builtin Python email library in my myapp/email.py file.

However, if I do:

from email import message_from_string

It of course, only finds itself, and therefore raises an ImportError on the method. Similarly, "import email" does the same when I try to call email.message_from_string() inside the module.

Is there any native support to do this in Python, or am I stuck with renaming my "email" module to something more specific?

thanks,

+10  A: 

You will want to read about Absolute and Relative Imports which addresses this very problem. Use:

from __future__ import absolute_import

Using that, any unadorned package name will always refer to the top level package. You will then need to use relative imports (from .email import ...) to access your own package.

Greg Hewgill