46

In python 2 I can create a module like this:

parent
->module
  ->__init__.py (init calls 'from file import ClassName')
    file.py
    ->class ClassName(obj)

And this works. In python 3 I can do the same thing from the command interpreter and it works (edit: This worked because I was in the same directory running the interpreter). However if I create __ init __.py and do the same thing like this:

"""__init__.py"""
from file import ClassName

"""file.py"""
class ClassName(object): ...etc etc

I get ImportError: cannot import name 'ClassName', it doesn't see 'file' at all. It will do this as soon as I import the module even though I can import everything by referencing it directly (which I don't want to do as it's completely inconsistent with the rest of our codebase). What gives?

That1Guy
  • 7,075
  • 4
  • 47
  • 59

2 Answers2

52

In python 3 all imports are absolute unless a relative path is given to perform the import from. You will either need to use an absolute or relative import.

Absolute import:

from parent.file import ClassName

Relative import:

from . file import ClassName
# look for the module file in same directory as the current module
Dunes
  • 37,291
  • 7
  • 81
  • 97
  • 2
    That works, figured it was some kind of search path issue. Chalking that up to the 'list of stuff we changed in python 3 just because.' Thanks much. –  Jul 07 '15 at 13:18
  • 2
    This a good change. It means you can have submodules that have the same name as a base module in python core library eg. `mymodule.io`, and all imports for `io` are fully specified and python doesn't arbitrarily import the wrong module because the ordering of the list of paths to import from has changed. – Dunes Jul 07 '15 at 13:22
  • 1
    Yeah I guess that's true, to me naming a module the same name as a base module is to avoided at all costs unless you're reimplementing it but I can see how avoiding 'oops we imported the wrong module' would be very useful. –  Jul 07 '15 at 13:47
  • I have a similar question in the context of gRPC auto-generated code (https://stackoverflow.com/questions/57213543/grpc-generated-python-script-fails-to-find-a-local-module-in-the-same-directory) . I am wondering why gRPC did not generate the "proper" code as shown in this answer (or PEP-328) ? – user1783732 Jul 26 '19 at 18:13
  • Never mind, I found a good answer and posted in the other question comments, thanks. – user1783732 Jul 26 '19 at 18:33
  • no body have `No module named '__main__.file'` here? – Cherry Nov 27 '19 at 10:34
  • @Cherry You should never import anything from `__main__` (the `.py` file that you invoke python with). Instead, move `file` to a separate module. Then have both your both `__main__` and your other module import `file` from the module you just created. There are a number of reasons why you shouldn't import from `__main__`, but a comment is to short to say why. Try asking a separate question. – Dunes Dec 02 '19 at 07:48
  • I'll upvote it if you will specifically mention that a relative import `import .foo` is prohibited. It took me quite some time to figure that out, because usually you just know you can do `import foo` as well as `from foo import bar`, but then it turns out that for relative imports [they special-cased that so `import .foo` will not work](https://peps.python.org/pep-0328/#:~:text=Relative%20imports%20must%20always%20use%20from%20%3C%3E%20import;%20import%20%3C%3E%20is%20always%20absolute). – Hi-Angel Oct 20 '22 at 14:33
  • @Hi-Angel If you think an answer (or question) can be improved then you should edit it. With your reputation the edit would take effect immediately rather than be a suggested edit. – Dunes Oct 22 '22 at 09:11
  • @Dunes IIRC such edits should not change semantics of a post. One can change grammar, formatting, broken or missing references… But adding a new content to someone's post may be against the author's wishes. That's why I just mention that there's more to it, rather than edit myself. Besides, I am new to all these importing nuances, but after spending whole day I've got a separate [note where I basically recommend against ever using relative imports](https://github.com/Hi-Angel/notes-backup/blob/master/python.md). Now that I think of that, I wonder if I should post that as a separate answer… – Hi-Angel Oct 22 '22 at 10:16
  • I wouldn't consider this to be a change of semantics. The main point of the answer is that all imports are absolute in python3 unless explicitly made to be relative. You're clarifying information in the answer (that relative imports must use the `from` form). – Dunes Oct 22 '22 at 12:48
32

Try import it this way:

from .file import ClassName

See here more info on "Guido's decision" on imports in python 3 and complete example on how to import in python 3.

doru
  • 9,022
  • 2
  • 33
  • 43