1.3.1 · D1Python Intermediate

Foundations — Modules — import, from…import, as aliasing

1,671 words8 min readBack to topic

Before you can read a single import line, you need to be fluent in the small pieces it is built from: what a file is, what a name is, what the dot does, and what a namespace is. This page builds each of those from nothing, in the order they stack on top of each other.


1. The .py file — the physical thing

Picture a folder on your computer. Inside it sit two text files, main.py and helper.py. Nothing magic — just text on disk.

Figure — Modules — import, from…import, as aliasing

2. A name — a label pointing at a value

Think of a name as a luggage tag tied by a string to a suitcase. The tag is x; the suitcase is 42. You can re-tie the same tag to a different suitcase later (x = 99).

Figure — Modules — import, from…import, as aliasing

3. A namespace — a box of tags

Picture two separate drawers. Your drawer has a tag pi on a suitcase holding 3 (you were sloppy). The math drawer has a tag pi on a suitcase holding 3.14159…. Same tag word, different drawers — no collision.

Figure — Modules — import, from…import, as aliasing

4. The dot . — "reach inside"

If math is a drawer, then math.sqrt is you saying "open the math drawer, take out the sqrt tool." The dot is the act of opening the drawer.


5. The keywords: import, from, as, *

Now that names, namespaces, and the dot exist, the keywords are just verbs acting on them.


6. sys.path and sys.modules — the two lists behind the scenes


Prerequisite map

py file on disk

a name points at a value

a namespace is a box of names

the dot reaches into a box

import from as star act on boxes

sys path finds the file

Modules topic import from import as

Read it bottom-up: files and names are the ground; namespaces group names; the dot reaches into a namespace; the keywords manipulate namespaces; sys.path tells Python which file to open. All of it feeds the Modules topic.


Connections

  • Namespaces and Scope — the "box of tags" picture, formalised.
  • sys and os modules — where sys.path and sys.modules live.
  • Python Functions — the most common thing a module hands you.
  • Packages and __init__.py — what happens when one drawer becomes a cabinet of drawers.
  • DRY Principle — the reason modules exist at all.

Equipment checklist

A .py file is — in plain words?
An ordinary text file ending in .py whose contents are Python instructions Python can run.
x = 42 means, read as an action?
"Point the tag/name x at the value 42" — it re-ties a label, it is not a maths equality.
A namespace is?
A box/container holding names, each pointing at a value; separate namespaces can reuse the same name without clashing.
The dot in math.sqrt does what?
Reaches into the namespace math and fetches the name sqrt from inside it.
After import math, which tags are in YOUR drawer?
Just one: math. To get sqrt you must use the dot: math.sqrt.
After from math import sqrt, which tag is in YOUR drawer?
sqrt itself, copied directly in — so no dot is needed.
What does as change — the suitcase or the tag?
Only the tag word (the nickname); the underlying object is exactly the same.
sys.path is?
An ordered list of folders Python searches to find the file to import; first match wins.
sys.modules is, and why it matters?
A cache of already-imported modules; it makes a module's top-level code run only once.