Python 101 · Modules and Runtime

Mar 5, 2016 | Tech Software

Index
Index

Import

  • Methods

    : Ways to import modules.
Method Characteristics Usage
Absolute Standard approach; searches from sys.path roots. Preferred for clarity and PEP 8 compliance. import os
os.path.join('a', 'b')
From Style Injects specific attributes directly into the local namespace. Reduces verbosity. from math import sqrt
foo = sqrt(16)
Alias Renames modules/functions to avoid naming conflicts or to shorten long names. import pandas as pd
df = pd.DataFrame()
Relative Uses . or .. for intra-package navigation. Only works within packages. from . import utils
utils.log("status")
Dynamic Loads modules at runtime via strings (could be from config files). Essential for plugin systems and lazy loading. import importlib
mod = importlib.import_module('sys')
Wildcard Imports everything not starting with an underscore. Highly discouraged; pollutes namespace. from math import *
bar = sin(3.14)
  • Combined and Local

    : Combination of multiple import methods.

Introspection

  • The ability of a program to examine its own structure, metadata, and properties at runtime.

Entry Point

  • A protective guard that ensures specific code runs only when the script is executed directly, not when it is imported as a module.