Python Importing

When you start to work on even rudimentary Python application, the first thing you usually do is import some package you’re using. There are many ways to import packages and modules - some are extremely common (found in pretty much every Python file ever written) and some less so. In this post I will cover different ways to import or reload modules, some conventions regarding importing, import loops and some import easter-eggs you can find in Python.


This is a companion discussion topic for the original entry at http://amir.rachum.com/blog/2013/10/10/python-importing/

Awesome. Is the third easter egg for me?

Weren't there changes in to the import system in Python 3? Perhaps you could make explicit which version(s) your article applies to.

This article applies to Python 2.7.
I added a "Python 2.7" tag to it.

This is the "python imports for dummies + relative imports" that I've been trying to find for over a year... THANK YOU :)

Thanks!

You forgot imp! http://docs.python.org/2/libra...

It's not easy to notice, and I wondered the same thing. You could mention it in the text.

Another use of relative imports is module reuse. This is quite common in Django apps and advocated in the Two Scoops Django book.

No importlib coverage? Puhleeeeze

Yes yes yes really really importlib should be in this, making it 288% more awesome than it is already. The __import__("foo") example is only the simplest case (module) and not the import of objects from a module.