liveBook · Manning

Import statement

As explained in Python Workout: 50 ten-minute exercises

An import statement is a source-code declaration that makes names or definitions from another module, package, or namespace available in the current scope.

Use an import statement to reuse definitions from another Python module instead of repeating code. Modules can hold data structures, functions, and classes, improving maintainability and reducing cognitive load. section 8.0

Choose the form that gives the current program the names it needs:

  • import os binds the module name os; use names through os, such as os.path. section 8.0
  • import mymod as mm and import numpy as np bind shorter aliases for modules. section 8.0
  • from os import sep binds only sep, while from os import sep, path binds both selected names. section 8.0
  • Avoid from os import * when explicit names make dependencies clearer. section 8.0

Split a tax calculation across two files

Put the reusable function in freedonia.py: def calculate_tax(amount, province, hour): ... Then load it in use_freedonia.py: from freedonia import calculate_tax print(calculate_tax(500, 'Harpo', 12)) The first file owns the calculation; the second imports and calls it. This keeps the implementation in one place while allowing multiple programs to reuse it. section 8.1

Loading a module executes its file from beginning to end. Python searches directories listed in sys.path, loads a module once, and records the loaded module object in sys.modules; later imports reuse it while defining the requested name. Guard executable code with if __name__ == '__main__': when it should run directly but not during import. section 8.5 section 8.7

Table 8.1 What you need to know (view table figure)

Concept

What is it?

Example

To learn more

import

Statement for importing modules

import os

http://mng.bz/xWme

from X import Y

Imports module X, but only defines Y as a global variable

from os import sep

http://mng.bz/xWme

importlib.reload

Re-imports an already loaded module, typically to update definitions during development

importlib.reload(mymod)

http://mng.bz/Z2PO

pip

Command-line program for installing packages from PyPI

pip install packagename

https://pypi.org/

Decimal

Class that accurately handles floating-point numbers

from decimal import Decimal

http://mng.bz/RAX0

Compare import forms with related module tools, including from X import Y, importlib.reload, pip, and Decimal. section 8.0

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains syntax, aliases, selected bindings, search paths, single loading, and execution guards, but does not detail import machinery internals.
Examples
Uses os imports, aliases, selected names, a two-file sales-tax program, and the __name__ execution guard.
What you can do after
Build it from scratch by separating reusable functions into modules and importing them with explicit or aliased names.
How it connects
Connects import statements with modules, packages, sys.path, sys.modules, standard library modules, and PyPI packages.

Also explained in

Compare how 13 books explain import statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.