riv
riv

Reputation: 7343

Python can't import from the same directory?

I have the following directory structure (I didn't write this so I'm assuming it has to work somehow?):

tool.py
core/
  __init__.py
  config.py
  common.py

tool.py indirectly imports config.py, and config.py has a line from common import foo, which displays the following error:

...
  File "...\core\config.py", line 5, in <module>
    from common import foo
ImportError: No module named 'common'

It probably isn't relevant, but I'm using Python 3.4 on Windows, and the tool.py directory is in the system path (I'm simply running it as tool).

Upvotes: 3

Views: 4540

Answers (2)

Konstantin
Konstantin

Reputation: 25369

You have to use relative imports

from .common import foo

Python 3 makes a distinction between absolute and relative imports and doesn't support implicit relative imports which you could use in Python 2.x.

Upvotes: 4

Daniel Roseman
Daniel Roseman

Reputation: 600041

Use a relative import:

from .common import foo

Upvotes: 5

Related Questions