close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I import from a file in the current directory in Python 3 in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

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

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

    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.

  2. import from a file in the current directory in Python 3

    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.

Method 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

Method 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.

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read