Locating and Executing Modules in Python

Python modules are files that contain Python code, including functions, classes, and variables. Modules help in organizing and reusing code. This guide will explain how Python locates and executes modules in detail.

1. Locating Modules in Python

When you import a module, Python searches for it in a specific order. The process follows these steps:

Step 1: The Built-in Modules

Before searching for external modules, Python checks its built-in modules (like math, sys, os, etc.). You can see the built-in modules using:

import sys
print(sys.builtin_module_names)

Output (Example)

('_abc', '_ast', '_thread', 'array', 'cmath', 'math', 'sys', 'time', 'os', ...)

If the module is in this list, Python loads it directly from memory.

Step 2: Checking sys.modules (Cache)

Python maintains a cache (sys.modules) of already loaded modules. Before searching externally, Python first checks if the module is already imported.

import sys
print("math" in sys.modules)  # Check if 'math' module is already loaded

Output:

False  # (If not imported before)

After importing:

import math
print("math" in sys.modules)

Output:

True  # Now it's cached

Step 3: Searching sys.path

If not found in built-in modules or cache, Python searches directories listed in sys.path.

import sys
print(sys.path)  # List of paths Python searches for modules

Order of Searching:

  1. Current Directory – First, Python checks the script’s directory.
  2. PYTHONPATH Environment Variable – If set, it searches in directories specified in PYTHONPATH.
  3. Standard Library Directories – Python searches in the standard library directories (Lib/ in Windows, /usr/lib/pythonX.X/ in Linux).
  4. Site-packages Directory – Python finally checks the site-packages directory (where third-party packages are installed).

Output (Example Paths)

[    '',    '/usr/lib/python38.zip',    '/usr/lib/python3.8',    '/usr/lib/python3.8/lib-dynload',    '/home/user/.local/lib/python3.8/site-packages']

Modifying sys.path to Include Custom Paths

import sys
sys.path.append('/my/custom/path')  # Add a new directory
print(sys.path)

2. Importing and Executing Modules

There are multiple ways to import and execute modules:

Method 1: Importing a Module

The simplest way to import a module:

import math
print(math.sqrt(25))

Output:

5.0

Method 2: Importing with an Alias

You can rename a module while importing.

import numpy as np
print(np.array([1, 2, 3]))

Output:

[1 2 3]

Method 3: Importing Specific Functions

Instead of importing the whole module:

from math import sqrt, pi
print(sqrt(16))  
print(pi)

Output:

4.0
3.141592653589793

Method 4: Importing Everything (*)

from math import *
print(sin(0))
print(log(10))

Output:

0.0
2.302585092994046

Warning: This can cause conflicts if two modules have functions with the same name.

Method 5: Executing a Module as a Script

If you have a module my_module.py:

# my_module.py
def greet():
    print("Hello from my_module!")

Importing and using it:

import my_module
my_module.greet()

Output:

Hello from my_module!

Method 6: Using __name__ == "__main__"

Use this check to differentiate execution as a script vs. import.

# my_script.py
def main():
    print("Executing my_script.py")

if __name__ == "__main__":
    main()

Executing Directly:

python my_script.py

Output:

Executing my_script.py

If imported, nothing happens.

Method 7: Dynamic Import Using importlib

import importlib
math_module = importlib.import_module("math")
print(math_module.sqrt(9))

Output:

3.0

3. Installing and Executing Third-Party Modules

Some modules must be installed using pip before use.

Command to Install:

pip install requests
import requests
response = requests.get("https://www.google.com")
print(response.status_code)

Output:

200  # HTTP status code (200 means success)

4. Handling Module Not Found Errors

If Python cannot find a module:

ModuleNotFoundError: No module named 'module_name'

Fixes:

  1. Check if the module is installed (for third-party modules).
  2. Verify sys.path includes the correct directory.
  3. Use virtual environments (venv or conda) to manage dependencies.

5. Creating and Using Your Own Modules

Step 1: Create my_module.py

# my_module.py
def add(a, b):
    return a + b

Step 2: Import and Use It

import my_module
print(my_module.add(2, 3))

Output:

5

6. Using Packages (Modules in a Directory)

A package is a collection of modules inside a directory containing an __init__.py file.

Example Structure:

my_package/
│── __init__.py
│── module1.py
│── module2.py

Importing a Package:

from my_package import module1

Conclusion

  • Python locates modules using built-in modules, sys.modules (cache), and sys.path (directories).
  • Modules are imported using import, from. import, or importlib.
  • If a module is missing, use pip install or modify sys.path.
  • Custom modules and packages help organize code efficiently.