Getting the Most Out of Python Imports

Audience level:
Core Python (Language, Stdlib)
March 10th 2:15 p.m. – 2:55 p.m.


To really take advantage of Python you must understand how imports work and how to use them effectively. In this talk we'll discuss both of these. After a short introduction to imports, we'll dive right in and look at how customizing import behavior can make all your wildest dreams come true.


Python's import statement has been a powerful feature since the first release, and only gotten better with age. Understanding how imports work under the hood will let you take advantage of that power.

A big key to customizing Python's imports is the importers introduced by PEP 302. That's a tool that you want in your belt! We'll be covering such import hooks as well as a couple other customization methods.

Talk Outline

  1. introduction and overview (2 min)
  2. under the hood (5 min)
    • import syntax
    • builtins.import
    • PEP 302
    • import state
  3. customizing Python (5 min)
  4. examples in the wild (5 min)
  5. 2 "walk-through" examples (5 min)
    • trying out new Python syntax
    • stat-averse imports
  6. 5 "flavor" examples (5 min)
    • descriptors for module objects
    • circumventing gotchas
    • sneaking directives into import names
    • managing multiple import hooks
    • context manager for sys.path
  7. "lightning" examples (5 min)
    • quick look at a bunch of working examples of cool stuff you can do with imports
    • here's a sample:
    • post-import hooks
    • a python preprocessor
    • naive macros and inline functions
    • packages win noisily
    • lazy imports
  8. conclusion

Supplemental Information

I have a lot more information at my Python Imports reference page.