How to Import Modules From Another Folder in Python?

When working on a Python project, you may need to import modules from another folder. For example, to add additional directories to Python for new user-defined modules and packages. There are several ways to import modules from another folder, such as adding a module path to the system path using various functions, using the importlib module, etc.

The following methods are provided to help you to import modules in Python from another folder:

  • Method 1: Using sys.path.append()
  • Method 2: Using sys.path.insert()
  • Method 3: Using the Relative Path to the Module
  • Method 4: Using importlib Module

Prerequisites:

The following folder/directory structure is used for this specific post:

The “sub_module.py” Python file (module) contains the various functions. 

Module FIle: sub_module.py

The function defined in the file named “sub_module.py” is shown below:

def add(a, b):
    return a+b

def sub(a, b):
    return a-b

def mul(a, b):
    return a*b

def div(a, b):
    return a/b

Now, we will create another file in which we try to import the previously created module. 

Main File: main.py

In the main file, the “sub_module” is imported at the program’s start.

import sub_module
# calling sub_module functions
print("Addition of two number is :", sub_module.add(2, 2))
print("Subtraction of two number is :", sub_module.mul(2, 2))

The above code throws an error because the module is directly imported from another folder which is not possible.

To solve this issue, Python uses different methods to import modules from another folder. 

Method 1: Using the sys.path.append()

The “sys.path.append()” method can be utilized to append a specific/particular path for a Python interpreter to search. The below code uses the “sys.path.append()” function to add the module path to the system path:

Code: 

import sys
sys.path.append(r'C:\Users\p\Documents\program\SUB')

import sub_module
# calling sub_module functions
print("Addition of two number is :", sub_module.add(2, 2))
print("Multiplication of two number is :", sub_module.mul(2, 2))
  • The module named “sys” is imported at the start of the program.
  • The “sys.path.append()” function takes the path of the module folder as an argument and adds it to the list of paths stored in “sys.path”.
  • After adding the path, now we can import the module of another folder using the import keyword and apply various functions.

Output: 

The functions of the modules stored in another folder have been applied successfully.

Method 2: Using the sys.path.insert()

The “sys.path.insert()” function is used to insert the path of the specific directory to the list of directories of Python. 

The syntax of sys.path.insert() is shown below:

sys.path.insert(index, path)

The index is where you want to insert the path, and the path is the directory path you want to include. Let’s import the module from another directory using the below code:

Code: 

import sys
sys.path.insert(1, r'C:\Users\p\Documents\program\SUB')

import sub_module
# calling sub_module functions
print("Subtraction of two number is :", sub_module.sub(2, 2))
print("Division of two number is :", sub_module.div(2, 2))
  • The module named “sys” is imported at the start.
  • The “sys.path.insert()” function takes the index and the complete path as an argument and adds the path to the system.
  • After adding now, we can import the module using the keyword “import.”
  • Lastly, the module’s function named “sub_module” is called on the given value.

Output: 

The function returns the subtraction and division of two numbers.

Method 3: Using the Relative Path to the Module

A relative path describes/represents the location of a file or a directory relative to the current/present working directory. The relative path to the module is used in the below code to import the modules from another folder:

Code: 

from SUB import sub_module

# calling sub_module functions
print("Subtraction of two number is :", sub_module.sub(2, 2))
print("Division of two number is :", sub_module.div(2, 2))
  • The “from SUB import sub_module” is used to import the sub_module from the folder “SUB”.
  • The sub() and div() function of the module named “sub_module” is used to find the subtraction and division of the given numbers.

Output: 

The function of the imported module has returned the values.

Method 4: Using the importlib Module

The importlib package provides the importlib.util module to import the module from the specified path. Here is an example code:

Code:

import importlib.util as iu
path = r'C:\Users\p\Documents\program\SUB'
name = 'sub_module'
spec = iu.spec_from_file_location(name, path + '\\' + name + '.py')
my_lib = iu.module_from_spec(spec)
spec.loader.exec_module(my_lib)
print('Addition of Number: ',my_lib.add(5,3))
print('Multiplication of Number: ',my_lib.mul(5,3))
  • The code first creates a module specification object by calling the “spec_from_file_location()” function by accepting the name and path of the module file.
  • Then, it creates a module object by calling “importlib.util.module_from_spec(spec)” with the specification object.
  • Next, it executes the module code by calling “spec.loader.exec_module(module)” with the module object.
  • Finally, the function named “add()” and “mul()” is applied on the given value.

Output:

The function has been called successfully.

Conclusion

To import modules from another folder, the “sys.path.append()”, “sys.path.insert()”,  “relative path to the module,” and “importlib” module is used in Python. The “sys.path.append()” and “sys.path.insert()” is used to add the specified module path to the Python path of directories. The relative path of the module is used to import the specified module from the subdirectory. 

This guide has presented various methods to import the modules from another folder using numerous examples.