<p>is looking for a module named <ttclass="docutils literal"><spanclass="pre">django.db.models</span></tt>. If the statement is
successful, <ttclass="docutils literal"><spanclass="pre">django.db.models</span></tt> will be a variable name in the scope,
it will be a Python <em>object</em> (of <ttclass="docutils literal"><spanclass="pre">class</span><spanclass="pre">Module</span></tt>, but that’s not
important), and it will have functions, class constructors, variables
and constants. These Python objects will be accessible through the dot
operator.</p>
<p>An alternative way of writing import statements is</p>
<p>And then the variable will just be <ttclass="docutils literal"><spanclass="pre">encoding</span></tt>. The <ttclass="docutils literal"><spanclass="pre">encoding</span></tt>
module has a function for handling unicode-to-web translation.
Accessing it through the dot operator, it looks like this:</p>
<h3><ttclass="docutils literal"><spanclass="pre">sys.path_hooks</span></tt>: How does Python know where to look?<aclass="headerlink"href="#sys-path-hooks-how-does-python-know-where-to-look"title="Permalink to this headline">¶</a></h3>
<p>That’s what’s funny. Python has two independent ways of making sense of
of the import string. The old system is based on the assumption that
everything is a filesystem, with folders and filenames. This is called
the <ttclass="docutils literal"><spanclass="pre">sys.path_hooks</span></tt> system.</p>
<p>In the old system, the parts of the import string would be split up, and
then a collection of directories would be scanned to see if the first
name in the import string could be matched with a subdirectory. If it
could, that directory would be scanned until the last name on the import
string. If that name was a <em>filename</em>, it would be loaded as a module.
If that name was a <em>directory</em> and that directory had a file named
<ttclass="docutils literal"><spanclass="pre">__init__.py</span></tt>, then that file would be loaded as the module.</p>
<p>The <ttclass="docutils literal"><spanclass="pre">sys.path_hooks</span></tt> array has a list of different methods for trying to
scan a filesystem for the parts of the import string. A <ttclass="docutils literal"><spanclass="pre">path_hook</span></tt> is
a function that takes a path to a directory; if it can handle the
contents of the directory, it returns a <strong>Finder</strong>, an object whose job
is to figure out how to load the module; if it can’t, it returns an
ImportError exception. The object that loads the module is called,
naturally, a <strong>Loader</strong>.</p>
<ulclass="simple">
<li>To read more about <strong>Finders</strong>, see <aclass="reference internal"href="eli5.html#eli5-finders"><em>Finders</em></a></li>
<li>To read more about <strong>Loaders</strong>, see <aclass="reference internal"href="eli5.html#eli5-loaders"><em>Loaders</em></a></li>
<li>To read more about <strong>Path Hooks</strong>, see <aclass="reference internal"href="eli5.html#eli5-pathhooks"><em>Path Hooks</em></a></li>
<h3><ttclass="docutils literal"><spanclass="pre">sys.path</span></tt>: What directories are searched?<aclass="headerlink"href="#sys-path-what-directories-are-searched"title="Permalink to this headline">¶</a></h3>
<p>The list of directories is stored in an array, <ttclass="docutils literal"><spanclass="pre">sys.path</span></tt>. This path is
initialized by Python when it starts up, but programs can modify it at
run-time to point to extra directories if they want.</p>
<h3><ttclass="docutils literal"><spanclass="pre">sys.meta_path</span></tt>: What is the new system?<aclass="headerlink"href="#sys-meta-path-what-is-the-new-system"title="Permalink to this headline">¶</a></h3>
<p>The new system is called <ttclass="docutils literal"><spanclass="pre">sys.meta_path</span></tt>, and it’s an array of
<strong>Finders</strong>, objects that have one method, <ttclass="docutils literal"><spanclass="pre">find_module(fullname)</span></tt>.
It’s an anything-goes API that gives developers the freedom to import
modules from anywhere: databases, archives, remote web resources, even
code written on-the-fly internally. The new system can apply any
meaning at all to the import string.</p>
<p>In Python, the import string is offered to each object in
<ttclass="docutils literal"><spanclass="pre">sys.meta_path</span></tt> before being offered to each <ttclass="docutils literal"><spanclass="pre">sys.path_hook</span></tt>. The
filesystem is typically the last finder tried.</p>
<p>To read more about <strong>Meta Paths</strong>, see <aclass="reference internal"href="eli5.html#eli5-metapaths"><em>Meta Paths</em></a></p>
<h3>Is it different between Python 2 and Python 3?<aclass="headerlink"href="#is-it-different-between-python-2-and-python-3"title="Permalink to this headline">¶</a></h3>
<p>Python 3 moves almost everything about this process into python’s
library, leaving only a bare minimum of functionality inside the Python
executable to load this library and run it. When the Python developers
did that, they added a lot of functionality to make it easier to write
new import modules. The old way still works, but there are now <em>Module
Specifications</em>, which are metadata about a module, and the old
<ttclass="docutils literal"><spanclass="pre">path_hooks</span></tt> system is now just a <ttclass="docutils literal"><spanclass="pre">meta_path</span></tt> handler added to the
new system as the last resort.</p>
<p>To read more about <strong>Module Specifications</strong>, see <aclass="reference internal"href="eli5.html#eli5-specs"><em>Module Specifications</em></a></p>
<h3><ahref="index.html">Table Of Contents</a></h3>
<ul>
<li><aclass="reference internal"href="#">Details on Import and Polyloader</a><ul>
<li><aclass="reference internal"href="#welcome-to-the-python-import-eli5">Welcome to the Python Import ELI5!</a><ul>
<li><aclass="reference internal"href="#what-is-import">What is <cite>import</cite>?</a></li>
<li><aclass="reference internal"href="#sys-path-hooks-how-does-python-know-where-to-look"><ttclass="docutils literal"><spanclass="pre">sys.path_hooks</span></tt>: How does Python know where to look?</a></li>
<li><aclass="reference internal"href="#sys-path-what-directories-are-searched"><ttclass="docutils literal"><spanclass="pre">sys.path</span></tt>: What directories are searched?</a></li>
<li><aclass="reference internal"href="#sys-meta-path-what-is-the-new-system"><ttclass="docutils literal"><spanclass="pre">sys.meta_path</span></tt>: What is the new system?</a></li>
<li><aclass="reference internal"href="#is-it-different-between-python-2-and-python-3">Is it different between Python 2 and Python 3?</a></li>
<li><aclass="reference internal"href="#does-the-old-system-still-matter">Does the old system still matter?</a></li>