AppDividend
Latest Code Tutorials

How to Import Class From Another File in Python

0

If your background is from the JavaScript world, you often use one javascript module inside another module very easily but if you try this approach on Python, it won’t work.

You can’t just export your class file and import the file whenever you want because the problem is that Python is looking in the Python directory for this file and not finding it.

You must specify which directory you are talking about and in which directory you are in and not the Python one. Let’s see how to solve the importing class error in Python.

Import class from another file in Python

Importing files from one folder to another in Python is a tricky task, and by default, you can’t do that, and if you try, you will get an error. The reason is that Python only searches the directory that the entry-point script is running from.

To import files from a different folder, you need to add the Python path at runtime. To add the Python path, use the sys.path.append() method, which includes locations such as the package.

The sys is an inbuilt Python module that contains parameters specific to the system.

The sys.path.append() is an inbuilt Python method that can be used with a path variable to add a specific path for interpreters to search.

To import a class from another file, you need to add Python path runtime using the sys.path.append() method, and then it will be able to resolve the importing file’s path.

Example of importing a class in Python

Let’s say, in our current working project directory, there is a folder called libs, and inside that folder, there is one Python file called add.py.

We want to use this add.py file in our app.py file.

Write the following code inside the add.py file.

def sum(x, y):
    return x + y

As the name suggests, it will return the addition of two numbers.

Now, open our main program file app.py and add the following code.

import sys

sys.path.append("/Users/krunal/Desktop/code/pyt/database/libs")

Here, we are appending Python path run-time using the sys.path.append() method and pass the destination path in which we have put our class file add.py.

Now, we can import the add.py file since now Python interpreter will know where to look for a class file.

import sys

sys.path.append("/Users/krunal/Desktop/code/pyt/database/libs")

from add import sum

We are importing the sum() function directly from the add module.

import sys

sys.path.append("/Users/krunal/Desktop/code/pyt/database/libs")

from add import sum

print(sum(5, 6))

Output

11

That is it. We successfully imported a file into our program and used it perfectly.

This approach works fine when you want to import from a “sibling” directory, so one up one down.

Importing class from a package in Python

To import a class from a package, use the following code.

from application.app.folder.file import function_name

Please make sure that the package folder contains an __init__.py, and this allows it to be included as a package. But when it is necessary to modify the Python path, you need to use the above approach and not use it.

If you structure your files like packages, then this approach will be effective. Otherwise, the first approach will be very much helpful. Use according to your requirements.

So, this is how you need to import a file from one directory to another or one class from another class.

That is it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.