6

So in a file foo I am importing modules:

import lib.helper_functions
import lib.config

And in helper_functions.py, I have:

import config

When I run the main function of foo I get an ImportError

EDIT: Here is the structure of the files I have

foo.py
lib/
    config.py
    helper_functions.py

The error results from importing config in helper_functions

Traceback (most recent call last):
  File "C:\Python33\foo.py", line 1, in <module>
    import lib.helper_functions
  File "C:\Python33\lib\helper_functions.py", line 1, in <module>
    import config
ImportError: No module named 'config'

So: when I run foo.py the interpreter is complaining about the import statements of helper_functions. Yet when I run the main of helper_functions, no such error appears.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Anas Elghafari
  • 1,062
  • 1
  • 10
  • 20
  • 3
    You need to import modules in every other file that uses them; each module gets its own namespace and doesn't inherit the names in the files it imports (unless you use the `from foo import *` syntax which you should avoid as it leads to horrible, hard-to-diagnose bugs) – Wooble Apr 14 '13 at 20:39
  • If you're saying I need to import config in foo, I am already doing that (of course in foo I have to refer to it as lib.config) It's not helping with the NameError. – Anas Elghafari Apr 14 '13 at 20:57
  • 2
    I don't see a *NameError* in your traceback. I see an `ImportError`, that is a very *different* error. – Martijn Pieters Apr 15 '13 at 10:58
  • Thanks for note, I actually re-produced the original setup minimally before I added the recent edit. So there was a NameError in my code, but that was coming from somewhere else. I guess I should edit it since it's not part of this particular problem. – Anas Elghafari Apr 15 '13 at 11:09

2 Answers2

7

You need to import config using an absolute import. Either use:

from lib import config

or use:

from . import config

Python 3 only supports absolute imports; the statement import config only imports a top level module config.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • But what is throwing the error is not importing lib.config into foo. Rather, it is importing config into helper_functions. However, curiously, this error is only when running foo.main not when running helper_functions.main. I edited the question to show the text of the error. – Anas Elghafari Apr 15 '13 at 10:45
  • @AnasElghafari: ah, I apologize, I did not read your question properly. Had you added the traceback to start with that would have been clearer. – Martijn Pieters Apr 15 '13 at 10:57
  • @AnasElghafari: Addressed your actual problem in an updated answer. – Martijn Pieters Apr 15 '13 at 11:00
  • @Marijn: Yep thanks. I changed the import statement in helper_functions so that it reads: from lib import config. And that fixed it. – Anas Elghafari Apr 15 '13 at 11:06
  • NOTE: While this solves the problem with foo (i.e. foo.py runs without an error), I can no longer run the __main__ of helper_functions (I use that for testing), since the interpreter will complain about `from lib import config` saying there is no module named "lib". At least that was the case for me. I think PYTHONPATH variable may play a role. – Anas Elghafari Apr 16 '13 at 16:29
  • Same thing: `ImportError: No module named lib` and `ValueError: Attempted relative import in non-package` – Luis A. Florit Jan 25 '14 at 18:51
  • You can only use relative imports within a package (a directory on your path with `__init__.py` in it), not between top-level modules. – Martijn Pieters Jan 25 '14 at 19:16
0

In python each module has its own namespace. When you import another module you are actually only importing its name.

The name "config" exists in the module helper_functions because you imported it there. Importing helper_functions in foo brings only the name "helper_function" into foo's namespace, nothing else.

You could actually refer to the "config" name in foo.py with your current imports by doing something like:

lib.helper_functions.config

But in python is always better to be explicit rather than implicit. So importing config in in foo.py would be the best way to proceed.

#file foo.py
import lib.helper_functions
import config
jujaro
  • 447
  • 2
  • 4
  • well, config is in lib. I am importing it in foo.py anyway (import lib.config). But when running foo.main, the interpreter complains about the import statements in helper_functions.py. even though when I run the helper_functions.main, no such error occurs. Anyway, I edited the question to add the error message – Anas Elghafari Apr 15 '13 at 10:50
  • Ah, I see your edit now. I couldn't reproduce it because I use Python2.7. This causes an error only in Python3. The reason has been explained by @Martijn Pieters in his answer. – jujaro Apr 20 '13 at 00:03