In this article, we will learn how to import a class from another file in Python. But there can be two scenarios when we try to import a class from another file. The file can be in the same folder or in a different one. So, we will take a look at both the scenarios.
So are you ready to dig more? Great! Then let us start right away!
Import class from another file in the same folder
So we have written a Python class in a file and now want to import it into another file in the same directory. How do we do that? I think this is best explained with an example.
So let me create a file called bird.py that has a Bird class in it. Alright? So this is how that class looks like:
//birds.py class Bird: def init(self, name, can_talk): self.name = name self.can_talk = can_talk def get_details(self): details = self.name + ', Can it talk? = ' + self.can_talk return details
So as you can see, the Bird class defined here will store the name of the bird and whether it can talk or not. Just a simple class, alright?
Now, say we want to import this class into my main Python file called main.py present in the same folder as shown below:
How do we go about doing that?
Well, since both our main.py and my bird.py files are in the same folder or directory, all we need to do is to import the bird.py’s Bird class in my main.py file. Hmm… confused? Well don’t be! Just take a look at the main.py file’s code below and you will get it!
//main.py from bird import Bird mybird = Bird('Parrot', Yes) print (mybird.get_details())
See? It is quite simple when both of the files are in the same directory. Right?
But what to do if your bird.py is in a different folder? That is what we will see in the next section.
Import class from another file in a different directory
Now in this case, let us put our bird.py in a sub folder called pet. So our directory structure looks something like this:
So how can we now import our Bird class into our main.py in this case? Well, now we need to make use of what is called a Python package.
What I mean by this is that we will now create our pet folder as a separate package and then use that package in any of our Python file present in any directory.
How to create a Python package
So we now want to create a package from our pet directory, right? How do we do that? Once again, it is quite a simple process in Python.
All you have to do to create a package in a folder using Python is to introduce a file called __init__.py. What this file does is to create a Namespace for the package. This is required so that you can use any class names in your package and not get conflicted with other packages using the same class name.
But here is the thing. You need this __init__.py file only if you are using a Python version that is version 3.3 or lower. But if you are using Python version higher than that, you dont even need to use the __init__.py file.
So, by adding a __init__.py file, our directory structure will now look something like this:
Now with this file in place, we can import the Bird class into our main.py file as follows:
from pet.bird import Bird mybird = Bird('Parrot', Yes) print (mybird.get_details())
So as you can see, once we have packaged the pet folder, calling Bird class from this package is done in line 1. Here we have used the dot operator to access the bird file present inside pet folder and then imported the Bird class.
The remaining code after that line is exactly the same as in our previous case.
So as you can see, importing classes from other files in Python is quite easy. It varies just a bit based on where the file is located. If it is in the same folder we can simply import the file. But if it is in another folder, we will have to create a package of that folder and then import it.
I hope this tutorial helped you in some ways in learning Python imports. But if you have any queries, please do let me know in the comments below and I will be more than happy to help.
So see you until next time!