Python classes are the approaches to utilize object-oriented programs in Python. Python enables to develop classes in the primary job we are dealing with or in a brand-new file that just shops the class meaning.
This short article will discuss how to import a class within the very same directory site or subdirectory in Python. For this short article, one does not require any Python modules however, a variation of Python 3. x.
In order to make use of directory sites in any Python job, it is essential to have an __ init __. py file which works as a router to work in between several files in a job. If one imports a module without an __ init __. py file, they will get a mistake as given up the following area.
Importing classes without __ init __. py
When we import classes from a directory/subdirectory without __ init __. py file in those directories/sub-directories, the python interpreter will toss the following mistake:
ImportError: tried relative import without any recognized moms and dad plan
Utilizing the __ init __. py file to import classes
>> CLASS #directory name
main.py #main file
name.py #file in very same directory site consisting of ‘name’ class> sub #sub- directory site
emp.py #sub- directory site with file emp.py including ’em tactic’ class
We will utilize this directory site established for presentation functions. Here, the moms and dad directory site CLASS has 2 files– main.py and name.py( includes name class), and a subdirectory sub which includes sub.py file which includes another class utilize. Now we will demonstrate how to import classes from name.py and emp.py into the main.py file.
To prevent the ImportError we saw in the previous area, we need to develop an empty __ init __. py file in the directory/sub-directory consisting of the needed class meaning. So, in our presentation folder, we require to develop 2 empty __ init __. py files; one in the moms and dad directory site CLASS and the other in the sub-directory sub.
The brand-new directory site structure would appear like this:
>> CLASS #directory name
__ init __. py #init apply for moms and dad directory site
main.py #main file
name.py #file in very same directory site consisting of ‘name’ class> sub #sub- directory site
__ init __. py #init apply for sub directory site
emp.py #sub- directory site with file emp.py including ’em tactic’ class
Following is the pictorial representation of the very same.

job structure
Here, we can see that CLASS is the primary directory site, the sub is a subdirectory consisting of __ init __. py and emp.py while the remainder of the files remain in the primary directory site. Now, the contents of the class meaning files are provided listed below.
Note: All the __ init __. py files are empty as they are just needed by the python interpreter for syntactical factors.
name.py
Python3
|
emp.py
Python3
|
Importing classes in main.py
In the main.py file, to import the other 2 files, we will utilize the following syntax.
Python3
|
The main.py file utilized in the example is as follows.
Python3
|
Here we utilize the from keyword to specify the file from which we are importing a class technique; then we import the class by following its name with the import keyword.
When it comes to the classes that exist in a sub-directory, we utilize the dot (.) indexing technique, which is,
<< sub-dir>>.<< filename>>
to specify the file place and after that import the needed class from it.
Output:

output
Note: If the __ init __. py file includes some other reference/definition for the imported classes, python will pick those over the ones in the moms and dad directory site and subdirectory.
Example 2:
In this example, we will see how to import classes at several levels of the directory site. Think about a circumstance where the class we require to import lies within a sub-directory, that itself lives inside a sub-directory. We will import a class that lives in a second-level sub-directory.
The directory site structure is as follows:

structure
We need to import the class situated inside the class_imp. py file into the main.py file. The class meaning in class_imp. py is:
Python3
|
The chauffeur program to import this class is as follows:
Python3
|
Output:

output
Last Upgraded:
24 May, 2023
Like Short Article
Conserve Short Article