Last modified: Mar 16, 2025 By Alexander Williams

Python importlib.machinery.SourceFileLoader() Guide

Python's importlib.machinery.SourceFileLoader is a powerful tool for dynamically loading Python source files. It allows you to load modules from source files without using the traditional import system. This can be useful in scenarios where you need to load modules dynamically at runtime.

What is SourceFileLoader?

The SourceFileLoader class is part of the importlib.machinery module. It is designed to load Python source files (.py files) and execute them as modules. This class is particularly useful when you need to load modules from non-standard locations or in a custom way.

To use SourceFileLoader, you need to provide the full name of the module and the path to the source file. The loader will then read the file, compile it, and execute it as a module.

How to Use SourceFileLoader

Here’s a simple example of how to use SourceFileLoader to load a Python source file:


import importlib.machinery

# Define the path to the source file
file_path = 'example_module.py'

# Create a SourceFileLoader instance
loader = importlib.machinery.SourceFileLoader('example_module', file_path)

# Load the module
module = loader.load_module()

# Now you can use the module
module.say_hello()

In this example, we load a module named example_module from the file example_module.py. The load_module() method reads the file, compiles it, and executes it as a module. After loading, you can call functions or access attributes defined in the module.

Example Code and Output

Let’s assume the content of example_module.py is as follows:


# example_module.py
def say_hello():
    print("Hello from example_module!")

When you run the previous code, the output will be:


Hello from example_module!

This demonstrates how SourceFileLoader can be used to load and execute a Python source file dynamically.

Key Points to Remember

When using SourceFileLoader, there are a few important things to keep in mind:

  • Module Name: The module name you provide must match the name used in the source file.
  • File Path: The file path must be absolute or relative to the current working directory.
  • Execution: The module is executed in its own namespace, so be cautious with global variables.

For more advanced usage, you might want to explore other functions like importlib.util.decode_source() or importlib.util.resolve_name().

Conclusion

The importlib.machinery.SourceFileLoader is a versatile tool for dynamically loading Python source files. It provides a way to load and execute modules without relying on the traditional import system. This can be particularly useful in scenarios where you need to load modules from non-standard locations or in a custom way.

By understanding how to use SourceFileLoader, you can take full advantage of Python's dynamic capabilities. For more information on related topics, check out our guides on importlib.resources.as_file() and importlib.invalidate_caches().