Home >Backend Development >Python Tutorial >How to Import Classes from Files within the Same or Subdirectory in Python?
In a directory containing Python files, how can classes from specific files be imported into another file within the same directory or subdirectory?
Python 2
Import classes using the regular import statement:
from user import User from dir import Dir
If the class files are in a subdirectory:
Use dot notation in the import statements:
from classes.user import User from classes.dir import Dir
Python 3
Similar to Python 2, but prefix the module name with a . if not in a subdirectory:
from .user import User from .dir import Dir
The above is the detailed content of How to Import Classes from Files within the Same or Subdirectory in Python?. For more information, please follow other related articles on the PHP Chinese website!