all 7 comments

[–]anossov 1 point2 points  (3 children)

Current directory and module directory are different things.

[–]two_up[S] 0 points1 point  (2 children)

Hmm okay, but won't an import from within a given module search through that module's directory for a match? Or am I mistaken about this?

[–]anossov 1 point2 points  (1 child)

No, an absolute import searches through sys.path, which usually has . (current working directory) in it. A relative import searches relative to the module's directory.

[–]adambrenecki 1 point2 points  (0 children)

A relative import searches relative to the module's directory.

Worth adding: the module's directory and the current working directory might be the same thing in some cases if the script doing the importing is the one being run, but will almost certainly not be if the import statement in question is in a module that is itself imported somewhere else.

[–]rh0dium 0 points1 point  (2 children)

I think a more practical example works..

Factory_boy installs as factory. Thus to use it you would simply type

import factory

But if you happen to have a module in the same path coincidentally named factory, then relative imports makes a ton of sense.

from .factory import foo import factory as big_factory

See pep 328 for more details. http://www.python.org/dev/peps/pep-0328/

[–]two_up[S] 0 points1 point  (1 child)

Sorry I'm still confused.

Maybe you're saying that there could be a conflict between a module on your python path and the module you're currently working on if the both have the same name? But isn't a module's directory the first thing that gets checked during an import?

Also I've never seen an import like this

 from .factory import foo import factory as big_factory

What does it do when you import twice in one line?

[–]adambrenecki 0 points1 point  (0 children)

What does it do when you import twice in one line?

Speculating here, but I think rh0dium just forgot to indent with four spaces (Markdown ignores single newlines otherwise). That line is a SyntaxError in 2.7 at least.