liveBook · Manning

Namespace

As explained in Python Workout, Second Edition

A namespace is a mapping that associates names with objects and separates those names from other naming contexts.

A namespace associates names with objects while separating those names from other naming contexts. In Python, each module has its own namespace, so collaborators can use similar variable or function names without collisions. Modules also provide a place to define reusable data structures, functions, and classes. section 9.0

Importing through a module namespace

Use a qualified module name when you want the module namespace to remain explicit: import os The import statement defines a variable, os, that references the module; it does not import a filename such as "os.py". Python searches directories in sys.path for a matching module. Alternatively, from os import sep defines sep directly in the current namespace, while import os keeps access qualified as os.sep. section 9.1

Table 9.1 What you need to know
Concept
What is it?
Example
To learn more
import
Statement for importing modules
import os
from X import Y
Imports module X, but only defines Y as a global variable
from os import sep
importlib.reload
Re-imports an already loaded module, typically to update definitions during development
importlib.reload(mymod)
pip
Command-line program for installing packages from PyPI
pip install packagename
Decimal
Class that accurately handles floating-point numbers
from decimal import Decimal

Use the reference table to compare import forms and related module tools, including import, from X import Y, importlib.reload, and pip. section 9.1

How Python Workout, Second Edition covers it

How deep it goes
Explains module namespaces, import-bound names, and filesystem lookup through sys.path, but stops short of deeper import internals.
Examples
Demonstrates import os, qualified module access, from os import sep, and module lookup through sys.path.
What it assumes
Assumes familiarity with Python variables and the LEGB rule while treating builtins as an existing namespace.
What you can do after
Apply it via Python's import statement to reuse definitions and choose qualified or direct name bindings.
How it connects
Connects namespaces with modules, packages, the import statement, builtins, and the LEGB rule.

Also explained in

Compare how 20 books explain namespace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.