Import the Class within the Very Same Directory site – Python

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 file #file in very same directory site consisting of ‘name’ class

> sub #sub- directory site #sub- directory site with file 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– and includes name class), and a subdirectory sub which includes file which includes another class utilize. Now we will demonstrate how to import classes from and into the 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 file #file in very same directory site consisting of ‘name’ class

> sub #sub- directory site
__ init __. py #init apply for sub directory site #sub- directory site with file 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 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.


class name():

def __ init __( self, name):

self name = name

def disp( self):

print( self name)


class utilize():

def __ init __( self, work):

self work = work

def task( self):

print( self work)

Importing classes in

In the file, to import the other 2 files, we will utilize the following syntax.


from << filename> > import << classname>>

from << sub- directory site>>.<< filename> > import << classname>>

The file utilized in the example is as follows.


from name import name

from sub.emp import utilize

per = name(" John")


emp = utilize(" Software Application Engineer")


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.



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:


We need to import the class situated inside the class_imp. py file into the file. The class meaning in class_imp. py is:


class Hey There():

def __ init __( self, name):

self name = name

def alert( self):

print( f' Hey there {}!')

The chauffeur program to import this class is as follows:


from level_2. level_3. class_imp import Hey There

individual = Hey There(" Geeks")




Last Upgraded:
24 May, 2023

Like Short Article

Conserve Short Article

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: