Open In App

Python - Import module outside directory

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, A module is a file containing Python code, functions, classes or variables that can be reused in other programs using import statement. Modules are usually imported from the same folder or a standard location. But sometimes, a module that needs to be used is placed in a different folder.

To handle this, Python allows adding the module’s path manually using different methods. This helps Python find and import modules that are outside current directory, making it easier to reuse code from different locations.

Sample Folder Structure:

D :/projects/base
|__main.py
|__app/
|__modules/
|___mod.py

ModuleFolderStructure

In the D:/projects/BASE/ directory:

  • main.py is located directly inside the BASE folder.
  • Inside BASE, there's a subfolder app, which contains another folder modules.
  • The modules folder contains mod.py, the file that will be imported into main.py.

Example:

Here, a simple hello() function is defined inside mod.py and the objective is to import and call it from main.py using various import techniques.

Python
# defining a function hello()
def hello():
    print('hello geeks!')

Now, Let's look at various methods to import module outside directory.

Using __init__.py

__init__.py method mark a folder as a Python package, allowing Python to recognize it as a valid source of modules. By placing an empty __init__.py file inside the folder you can import a module using dot notation (e.g., import app.modules.mod).

This approach is clean, follows Python’s standard import system and helps organize large projects without manual path changes.

D :/projects/base
|__main.py
|__app/
|__modules/
|___mod.py
|___init__.py

adding_initfile
Adding __init__.py file

Example:

This code imports a module using dot notation after converting the folder into a package with __init__.py.

Python
import app.modules.mod         

# calling the hello function of the module
app.modules.mod.hello()

Output

hello geeks!

Explanation:

  • import app.modules.mod: Imports mod.py using dot notation through the app.modules package (enabled by __init__.py).
  • app.modules.mod.hello(): Calls the hello() function from mod.py and prints its output.

Using importlib library

importlib library allows dynamic importing of modules from any file path even outside the current directory. Using importlib.util, modules can be loaded without modifying sys.path or making the folder a package, making it ideal for standalone or customized setups.

Example:

In this Example importlib.util is used to dynamically load and import a module from an external file path, allowing access to its functions like hello() without modifying system paths.

Python
import importlib.util

# Define module name and file path
path = "D:/projects/base/app/modules/mod.py"
mname = "mod"

# Load module from specified file location
spec = importlib.util.spec_from_file_location(mname, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)

# Call the hello function from the imported module
mod.hello()

Output

hello geeks!

Explanation:

  • spec_from_file_location() creates a module specification using module name and path.
  • module_from_spec() loads the module based on the spec.
  • exec_module() executes the module so it can be used.

Using sys.path.insert()

sys.path.insert() allows Python to import modules from directories outside the current one by adding their path to the sys.path list, which stores all module search locations. Adding the path at position 1 gives it priority, enabling clean imports without altering the folder structure.

Example:

This program allows Python to import a module from another directory by adding that directory to the module search path using sys.path.insert().

Python
import sys

# Add the path of the directory containing mod.py to sys.path
sys.path.insert(1, "D:/projects/base/app/modules")

# Import the module
import mod

# Call the hello function from the module
mod.hello()

Output

hello geeks!

Explanation:

  • sys.path.insert(1, ..) adds the mod.py folder to Python's search path with high priority.
  • import mod imports the external module.
  • mod.hello() calls hello() function from the imported module.

Using sys.path.append()

This method allows importing a module from another directory by using sys.path.append("path"), which adds the directory to Python’s module search path. It enables Python to locate and import the module without modifying the folder structure.

Example:

This program shows how to import a module from another directory by adding its path to Python's search path using sys.path.append().

Python
import sys

# Add the directory containing mod.py to the module search path
sys.path.append("D:/projects/base/app/modules")

# Import the module
import mod

# Call the hello function from the module
mod.hello()

Output

hello geeks!

Explanation:

  • sys.path.append() adds folder containing mod.py to the end of Python’s module search path.
  • import mod imports mod.py file as a module.
  • mod.hello() calls the hello() function defined inside mod.py.

Related Articles:


Article Tags :
Practice Tags :

Similar Reads