Skip to content Skip to sidebar Skip to footer

How Do I Import From A File In The Current Directory In Python 3?

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 wor

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

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

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

Post a Comment for "How Do I Import From A File In The Current Directory In Python 3?"