liveBook · Manning

Chapter 10: Modules and scoping rules

What you will learn in chapter 10 of The Quick Python Book, Fourth Edition

This introduction explains why modules are useful for organizing larger or reusable Python code.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why modules are useful for organizing larger or reusable Python code.

Key terms: module, Python standard library

10.1 What is a module?

This section defines Python modules, explains how their namespaces prevent name clashes, and shows how to make a module available in Colaboratory.

Key terms: module, Python source code, compiled modules, namespace, identifiers, name-clash problems, mymodule.reverse, othermodule.reverse, standard Python functions, modules

10.2 A first module

The section builds a small Python module and shows how to import its names, keep them distinct through qualification, and reload changes during interactive development.

Key terms: module, .py, import statement, qualification, attribute, mymath.pi, mymath.area, from modulename import attribute, from mymath import pi, reload

10.3 The import statement

This section explains three forms of Python’s import statement, how each makes module names available, and why wildcard imports require care.

Key terms: import statement, module name, from ... import ..., from modulename import *, public names, __all__, name clash

10.4 The module search path

This section explains how Python uses sys.path to locate imported modules and how that path is initialized.

Key terms: sys.path, module search path, import statement, ImportError, PYTHONPATH, script, interactive session, current directory

10.4.1 Where to place your own modules

This section explains where to put your own modules so programs can import them, and how different ways of extending Python’s search path affect which modules are found.

Key terms: sys.path, current directory, mymath module, site-specific modules, module search path, local code directories, PYTHONPATH, .pth file, sys.prefix

10.5 Private names in modules

This section explains how leading underscores keep module names out of wildcard imports without making them inaccessible.

Key terms: from module import *, leading underscores, private names, internal names

10.6 Library and third-party modules

This section distinguishes standard-library modules, which come with Python, from third-party modules, which must be installed before they can be imported.

Key terms: standard Python distribution, library modules, Python standard library, import, third-party modules, Python Package Index, module search path

10.7 Python scoping rules and namespaces

This section explains how Python namespaces bind names to objects, how name lookup works, and how bindings differ between functions and top-level code.

Key terms: namespace, identifier, binding, local namespace, global namespace, built-in namespace, parameter, module, __builtins__, list

10.7.1 The built-in namespace

This subsection shows how built-in names can be overridden and how namespace-inspection tools can help identify names in use.

Key terms: built-in namespace, binding, list, del, dir, module, local namespace, identifiers

10.8 Creating a module

This section asks the reader to turn earlier functions into a reusable module, test it from a new notebook, and upload it to Colaboratory if needed.

Key terms: standalone module, main program, Jupyter notebook, Colaboratory, file folder icon, upload document icon

10.8.1 Solving the problem with AI-generated code

This section explains how to guide an AI tool to repackage existing functions into a module while keeping the code that uses them separate.

Key terms: module, import, client code, prompt

10.8.2 Solutions and discussion

10.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 9: Functions Chapter 11: Python programs →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.