liveBook · Manning

Import statement

As explained in Learn AI-Assisted Python Programming, Second 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 a module’s code available to a Python program. Modules group code for particular purposes, so importing one extends Python beyond its core features without requiring knowledge of its internal implementation. section 5.1.5

Use imports to:

Fixing a missing import

A password checker that uses string.punctuation fails with NameError when string has not been imported. Adding import string at the start makes the punctuation test work: passwords without punctuation return False, while 'N3w Y0rk J375$' returns True. section 3.3.2

Importing modules for a PDF tool

A PDF-cover utility calls os.listdir and os.path.join, so it needs import os. It also imports a PDF library. When PyPDF2 3.0.0 rejects the older PdfFileReader name, the code must use PdfReader, install an older compatible version, or switch to PyMuPDF. section 9.4.2

Table 5.2 Summary of commonly used Python modules
Module
Built-In
Description
csv
Yes
Aids in the reading, writing, and analysis of CSV files
zipfile
Yes
Aids in the creation and extraction of compressed zip archive files
matplotlib
No
Graphics library for plotting that serves as the basis of other graphics libraries and can offer high levels of customization
plotly
No
A graphics library used for creating interactive plots for the web
seaborn
No
A graphics library built on top of matplotlib that aids in creating high- quality plots more easily than matplotlib
pandas
No
A data processing library that specializes in data frames, which are analogous to spreadsheets
scikit-learn
No
Contains basic tools for machine learning (i.e., helping to learn from data and make predictions)
numpy
No
Offers highly efficient data processing
pygame
No
A game programming library that helps to build interactive, graphical games in Python
django
No
Web development library that aids in designing websites and web applications

Browse a compact comparison of built-in and external Python modules and the tasks they support. section 5.1.5

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains why imports are needed, distinguishes built-in from installable modules, and diagnoses missing-import and version errors without covering import internals.
Examples
Uses string.punctuation for password validation, zipfile for archives, os for directories, and PyPDF2 or PyMuPDF for PDF processing.
What it assumes
Assumes basic Python functions and files while teaching module purpose, built-in versus external availability, and import troubleshooting.
What you can do after
Operate/configure Python programs by importing suitable modules, installing external dependencies, and fixing missing-name or compatibility errors.
How it connects
Connects modules with packages, functions, the standard library, installation, file handling, and dependency-version compatibility.

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 Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.