Python Module Imports - Explicit vs Implicit Relative Imports

Viewed 5975

Last night, when working on my mac, I set up some module imports in my __init__.py's

from MongoProvider import MongoProvider
from Settings import Settings

etc. I'm unsure of what version of Python is on that machine. I'll edit the question later with that info once I have it.

Today, working on a different machine, which is Windows and using Python 3.3.3, my module imports were breaking. By adding an explicit relative import (adding a leading dot), I was able to fix the issue.

from .MongoProvider import MongoProvider
from .Settings import Settings

The trace I'm receiving is:

Traceback (most recent call last):
File "app.py", line 5, in <module> from modules.route_handlers import Route_Handlers
File "C:\Users\willb\bearded-dubstep\modules\route_handlers\Route_Handlers.py", line 9, in <module> from modules.backend_providers import Settings
File "C:\Users\willb\bearded-dubstep\modules\backend_providers\__init__.py", line 1, in <module> from MongoProvider import MongoProvider
ImportError: No module named 'MongoProvider'

My Project Layout is

root
|_modules
  |_api_helpers
    |  __init__.py
    |  InvalidUsage.py
    |  response_utils.py
  |_backend_providers
    |  __init__.py
    |  MongoProvider.py
    |  Settings.py
  |_route_handlers
    |  __init__.py
    |  Route_Handlers
|  app.py

Any ideas what would cause this? Is there a configuration file I should be looking at?

2 Answers
Related