2

I have a file:

STARTDIR/module/submodule/config.py

I have another file:

STARDIR/utils/filesys/getAbsPath.py

Why does this line work, in config.py?

from ..utils.filesys import getAbsPath

It seems like .. refers to module, not STARTDIR. There is no utils in module at all. In fact, doing

from .. import utils

yields

ImportError: cannot import name utils
Claudiu
  • 224,032
  • 165
  • 485
  • 680

1 Answers1

4

This should work:

from ...utils.filesystem import getAbsPath

This is because:

  • from . import … imports from STARTDIR/module/submodule/
  • from .. import … imports from STARTDIR/module/
  • from ... import … imports from STARTDIR/
David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • it won't, since i'm starting my script in `STARTDIR` (so it would be beyond a toplevel module import error). but i've re-arranged my code to have that work. – Claudiu May 25 '11 at 15:33
  • What do you mean, “since I'm starting my script”? Like, because STARTDIR isn't a Python module? – David Wolever May 25 '11 at 16:01
  • yeah. the script i'm running is in `STARTDIR/run.py`. even if there is `STARTDIR/__init__.py`, `STARTDIR` won't be a module. – Claudiu May 25 '11 at 16:15
  • Ah, ok. In that case, as you've probably figured out, you can't use relative imports. Again, as you've probably figured out, the simplest thing to do is to add STARTDIR to your PYTHONPATH, then just `import utils.…`. – David Wolever May 25 '11 at 16:36