How does python import modules from some other directory?

While working on big projects we may confront a situation where we want to import a module from a different directory. But for some reason, the module may not be imported correctly. Now don’t worry if your module is not imported correctly. In this article, we will discuss ways to import a module from another directory. 

Note: A module is just a Python program that ends with .py extension and a folder that contains a module becomes a package.

Directory Structure:

 - Folder_1
    - main.py
 - Folder_2
     - module1.py

Let’s suppose, to import how to import file in Python, we have two different folders, one contains main.py which is our main Python file where we want to import module1 from Folder_2. 

Module1: contains two functions called add and odd_even. The function add will takes two arguments and return the addition of them. The odd_even function will take only one argument and print Even if the number is even or print Odd if the number is odd.

module1.py 

Python3

def add(a, b):

    return a+b

def odd_even(n):

    if n % 2 == 0:

        print("Even")

    else:

        print("Odd")

If we simply try to import module1 from Folder_2, we will be encountering the following error.

main.py

Python3

import Folder_2

module1.odd_even(5)

Output:

How does python import modules from some other directory?

Error

ModuleNotFoundError, because by default Python interpreter will check for the file in the current directory only, and we need to set the file path manually to import the modules from another directory. We can do this using various ways. These ways are discussed below in detail.

Method 1: Import module from different directory using the sys module

We can use sys.pathto add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn’t find the module in its current directory. As sys.path falls under the list type class so, we can easily use the insert method to add the folder path.

Python3

from module1 import odd_even, add

import sys

sys.path.insert(0, '/home/amninder/Desktop/Folder_2')

odd_even(5)

print("Addition of two number is :", add(2, 2))

Output:

How does python import modules from some other directory?

Using sys

Method 2: Using the PYTHONPATH environment variable

Similarly, if you don’t want to use the sys module to set the path of the new directory. You can assign a directory path to the PYTHONPATH variable and still get your program working. 

In Linux, we can use the following command in the terminal to set the path:

export PYTHONPATH=’path/to/directory’

In the Windows system :

SET PYTHONPATH=”path/to/directory”

To see if the PYTHONPATH variable holds the path of the new folder, we can use the following command:

echo $PYTHONPATH

Python3

from module1 import odd_even, add

odd_even(5)

print("Addition of two number is :", add(2, 2))

Output:

How does python import modules from some other directory?

Using PYTHONPATH

Suppose we have a directory structure like this:

- project
    - Folder_1
        - main.py
    - Folder_2
        - subfolder
            - new.py

Now, you want to import the new.py module from Folder_2 to our project’s Folder_1 main.py file.

Syntax:

 from project.folder.subfolder.filename import functionname

Python3

import sys

sys.path.insert(0, '/home/amninder/Desktop/project/Folder_2/subfolder')

from new import hello

hello()

Output:

How does python import modules from some other directory?

Output


How are Python modules imported?

Importing Modules To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.

How do I run a Python module from another directory?

We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.

How do I import a module from a previous directory?

In order to import a module, the directory having that module must be present on PYTHONPATH. It is an environment variable that contains the list of packages that will be loaded by Python. The list of packages presents in PYTHONPATH is also present in sys. path, so will add the parent directory path to the sys.

How is module imported to the program?

You need to use the import keyword along with the desired module name. When interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot(.) operator along with the module name.