How do I import from a file in the current directory in Python 3?

77,311

Solution 1

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

Solution 2

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.

Share:
77,311
Admin
Author by

Admin

Updated on July 08, 2020

Comments

  • Admin
    Admin almost 4 years

    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?

  • Admin
    Admin almost 9 years
    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.
  • Dunes
    Dunes almost 9 years
    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.
  • Admin
    Admin almost 9 years
    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.
  • user1783732
    user1783732 almost 5 years
    I have a similar question in the context of gRPC auto-generated code (stackoverflow.com/questions/57213543/…) . I am wondering why gRPC did not generate the "proper" code as shown in this answer (or PEP-328) ?
  • user1783732
    user1783732 almost 5 years
    Never mind, I found a good answer and posted in the other question comments, thanks.
  • Cherry
    Cherry over 4 years
    no body have No module named '__main__.file' here?
  • Dunes
    Dunes over 4 years
    @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.