Master Python Import from Another Directory Easily

There might arise a situation wherein you need to import a module from another directory. You might get an error or fail to do so if you have used the wrong import statement. By the end of this article, you’ll understand how you can easily import modules installed in other directories in Python.

Using sys.path

You can use the sys module to import from a different directory in Python. Here, with the .insert() function, we have to specify the path of the folder whose function definitions or any such functionality we wish to use.

Using sys.path with insert()

import sys
sys.path.insert(0,'location of folder from where you will import')

Using sys.path with append()

Directly append the location of the directory, which has the module whose classes and functions you need to use.


Using pythonpath

We can also use the pythonpath variable. This is executed in the terminal. Pythonpath is entered in either Linux or Windows. For Windows, type the below-mentioned statement along with the directory whose modules you want to use in the Python program.

SET PYTHONPATH="Resultantpath" 

For Linux, instead of SET, use EXPORT. You can echo the command to verify the results.


Using importlib.util

Assume that file1 is the name of the directory/file to be imported. Also, loc_file provides the path of the file. Now, import this library. After this, pass the file name and filepath as arguments, save it as file2( another variable), and execute its functions.

import importlib.util        
import_file = importlib.util.spec_from_file_location(
"file1", "")    
# arguments shared are 

Using __init__

__init__ method makes importing easier for the programmer. Just add an file in your directory, and you’re good to go. You can simply use the import module later on.

Import file using relative path

Here, .. implies using the directory above the given directory (in the hierarchy)

from ..module import

Import python file in databricks

You can quickly import a file while working with Databricks. Import the necessary modules- os and sys, in this case, and append the desired file path to the current file path.

import os
import sys
sys.path.append(os.path.abspath('file path'))

Import class from a different directory

To import a class from a file that is present in a different directory, either add an folder in that directory or use the sys command.

import sys
#specify the location of the file from which you have to import 
# here, .. means one directory above the current directory i.e. accessing the main directory from the current directory 
from main_directory.subfolder import *
#with import * One can access all classes of that folder

Import Python file from another directory in jupyter

In case you are working on a jupyter notebook, as mentioned above, you can use the method or sys module method to import files from another directory. Look at the following code snippet for a better understanding:

import sys  
sys.path.insert(0, 'file path')
import my_file
#enter the correct absolute path of the file in place of the file path, and make sure to import the needed file. This will ensure importing all the file related functions that you want.

Import from another directory in the unit test

While unit testing, you might want to execute a piece of code whose directory differs from the one you’re working in or to whom you have added the code for testing. Look at the following example to interpret importing from a different directory while using unit tests in Python.

import sys, os
my_Path = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, my_Path + '/../')
from file_name import module_name_or_class_name
# Use the sys module to insert the required file path that you got using the os.path method.
#after this, you can easily import from the required file.

See Also: init Python | Is __init__ in Python a Constructor?(Opens in a new browser tab)


How can I check whether the directory has been correctly imported?

You can check it using the print statement:


In this article, we learned several methods of importing a file from another directory.

Notify of
Inline Feedbacks
View all comments