Modules and import

The forms of import, what each one puts in your namespace, and why import * is discouraged.

Overview

The three forms

import math            # math.sqrt(9)
from math import sqrt  # sqrt(9)
import numpy as np     # np.array(...)

The first keeps the module as a prefix. That is a feature: reading math.sqrt a hundred lines later tells you immediately where it came from, and it cannot collide with anything of yours.

The second is shorter and right when you use one or two names heavily and there is no ambiguity.

as renames on the way in, for length (numpy as np) or to avoid a clash with a name you already have.

imports.py

imports.py Python 3
Output

                    

import_care.py

import_care.py Python 3
Output

                    

Worth knowing

import math keeps the module name as a prefix, which shows where a function came from.
from math import sqrt puts sqrt straight into your namespace.
import * hides where names came from and silently overwrites your own.
Imports are cached: a module runs once per program, no matter how often it is imported.

Modules and import: A Practical Guide

A module is a file of Python. import runs it once and gives you access to what it defines. The forms of import differ only in what ends up in your namespace, and that difference matters more than it first appears.

import * and why not

from math import *

This pulls in every public name at once. Two problems, and the second is the serious one.

You can no longer tell where a name came from. sqrt(16) appears from nowhere, and finding its source means guessing which of the star-imports supplied it.

Worse, it silently overwrites. If you defined gamma and then star-import a module that also defines gamma, yours is gone with no warning at all — the page demonstrates precisely that, printing the function's own output before and after.

The place it is acceptable is an interactive session where you are exploring, and even there it is a habit worth not forming.

Imports are cached

Importing a module twice does not run it twice. Python keeps a table in sys.modules and hands back the same module object. So imports are cheap to repeat, and any code at the top level of a module runs exactly once per program — which is why putting slow work or side effects at module level is a trap.

Where imports go

At the top of the file, one per line, standard library first, then third-party, then your own. That is not aesthetics: an import buried inside a function runs on every call and hides a dependency from anyone scanning the file.

The standard library is large

Before installing anything, check what ships with Python. collections, itertools, datetime, json, random, statistics, pathlib and re cover an enormous amount of everyday work, and the page uses three of them in six lines.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What is the main problem with `from module import *`?

  2. What does `import math` put in your namespace?

  3. Importing the same module twice does what?

Cheat sheet

Modules and import

A module is a file of Python. import runs it once and gives you access to what it defines. The forms of import differ only in what ends up in your namespace, and that difference matters more than it first appears.

PYTHON · vizlearn.in/python/modules_and_import.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.