Import Python Module Given Full Path



Importing Python Modules by Full Path

Python has various tools to import modules dynamically, where the full path is accessible. In this article, we will explore various methods to import Python modules when the full path is given, and each method has its own advantages for various use cases.

In Python, it is common to import modules using statements such as import os or from mypackage, import utils. But, Imagine a situation, Where we need to import the Python module given the full path, Let's dive into the article to learn more about it.

Using importlib.util.spec_from_file_location()

The Python provides functions for dynamically loading modules, especially from custom locations on our file system, with the help of the function importlib.util.spec_from_file_location().

Following is the syntax to imporrt the function named spec_from_file_location() -

importlib.util.spec_from_file_location(module_name, module_path)

Where,

  • module_name: A string that gives a name to the module, which can be arbitrary but unique within our session.
  • module_path: The full path to the .py file you want to load.

Following is an example -

import importlib.util

def import_module_by_path(module_path):
   module_name = "custom_module"  # Provide a custom name for the module

   spec = importlib.util.spec_from_file_location(module_name, module_path)
   custom_module = importlib.util.module_from_spec(spec)
   spec.loader.exec_module(custom_module)

   return custom_module

Using imp.load_source()

The Python importlib module was introduced in Python 3.5, before this, the imp module was commonly used for dynamically loading modules, especially from file paths. One of its key functions was imp.load_source(), which allows loading a Python module from a specific file location.

The syntax of the function imp.load_source() is as follows -

imp.load_source(module_name, module_path)

Where,

  • module_name: A string representing the name we want to assign to the loaded module.
  • module_path: The full path to the .py file we want to load.

Below is the example in which we use the imp.load_source() function to import the files with the full file path -

import imp

def import_module_by_path(module_path):
    module_name = "custom_module"  # Provide a custom name for the module
    custom_module = imp.load_source(module_name, module_path)
    return custom_module

After loading the module, we can use its functions or classes just like any other imported module.

The imp module has been deprecated since Python 3.4 and was removed in Python 3.12. For modern browsers, prefer using importlib.util instead.

Using importlib.machinery.SourceFileLoader()

Another way to dynamically import a Python module from a file path is by using importlib.machinery.SourceFileLoader. This approach gives low-level access to the import machinery and is useful when we want full control over how modules are loaded.

The following is the syntax of SourceFileLoader class usage is as follows -

importlib.machinery.SourceFileLoader(module_name, module_path).load_module()

Where -

  • module_name: A string that represents the name we want to assign to the module.
  • module_path: The full path to the .py file we want to import.

Here is an example function that uses SourceFileLoader to import a module from a given path -

from importlib.machinery import SourceFileLoader

def import_module_by_path(module_path):
    module_name = "custom_module"  # Provide a custom name
    custom_module = SourceFileLoader(module_name, module_path).load_module()
    return custom_module

When we run the above program, it loads the module and returns as a usable object in our Python environment.

The load_module() method got deprecated in future versions. For modern and forward-compatible code, it is recommended to use importlib.util.spec_from_file_location() with exec_module().

Using runpy.run_path()

The Python providesmethod called runpy.run_path() for executing Python scripts (as they were the main program), without needing to import them.

This is used when we want to run a script and retrieve its global namespace, such as variables, functions, etc., after execution. Here is the syntax of the function runpy.run_path() is as follows -

runpy.run_path(path_name, run_name="__main__")

Where,

  • path_name: The full path to the Python script we want to execute.
  • run_name: The name to assign to the __name__ variable in the executed script's context, and the default value is "__main__".

Here is an example function using runpy.run_path() -

import runpy

def execute_script_and_get_namespace(script_path):
   result_namespace = runpy.run_path(script_path)
   return result_namespace
	
namespace = execute_script_and_get_namespace('myscript.py')
print(namespace['some_function']())

On running the above program, It returns a dictionary containing the global variables defined in the script. The following are the situations when we can use runpy.run_path() -

  • Execute a script without permanently importing it
  • Access the script's global variables and functions after execution
  • Mimic the behavior of running a script via python script.py
Updated on: 2025-04-17T18:19:19+05:30

25K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements