liveBook · Manning

Import statement

As explained in The Quick Python Book, Fourth Edition

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

An `import` statement makes code from another Python module available in the current code. A module can be a `.py` file you wrote or a library module. Import the module before using its functions or other definitions. section 10.2 section 10.6

Choose the form that matches how you want to use the names:

  • `import mymath` loads the module; use qualified names such as `mymath.pi` and `mymath.area(2)`. Importing it does not make bare `pi` or `area` available. section 10.2 section 10.3
  • `from mymath import pi` makes `pi` directly available, but does not import `area`. You can name several attributes in one statement. section 10.2 section 10.3
  • `from mymath import *` makes exported names directly available. Normally this excludes names beginning with `_`; a module’s `__all__` list can instead specify which names it exports. section 10.3

Keep the module name visible

`import cmath` followed by `cmath.sqrt(-1)` produces `1j`. Keeping `cmath.` on the call distinguishes the complex-number function from similarly named functions in `math`. If both modules are imported with `*`, later imports can replace earlier names; wildcard imports also make a name’s origin harder to see. section 4.7.5 section 10.3

Python must be able to find the module: a local file can be imported when it is in the current directory or on the module search path, and third-party modules must be installed somewhere on that path. A missing module raises an error. Editing an already imported file does not bring its changes into the same interactive session; `importlib.reload(mymath)` reloads it. section 3.5 section 10.2 section 10.6

Packages group modules under hierarchical names. Importing a top-level package does not automatically load every submodule: import the one you need, such as `import mathproj.comp.numeric.n1`. Importing it loads its parent packages and executes their `__init__.py` files. Files within a package must also import definitions they use from sibling files. They may use an explicit package path or a relative import such as `from .n2 import h`; relative imports do not work in a module executed as `__main__`. section 18.3.3 section 18.3.4

When a file should work both as an importable module and as a script, put script-only calls under `if __name__ == '__main__':`. Importing the file then leaves those calls unexecuted. section 11.1.3

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains three import forms, name qualification, wildcard exports, package imports, and reload; defers detailed module-search locations.
Examples
Uses a circle-area module, `math` versus `cmath`, and a nested package with initialization output.
What it assumes
Assumes familiarity with functions and files while introducing module imports and qualified access through examples.
What you can do after
Build it from scratch: split functions into a module and call them from client code using an appropriate import form.
How it connects
Connects imports to modules, namespaces, packages, relative imports, and the `__main__` guard.

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 The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.