Appendix A · Installing packages.
Create an isolated environment and know where an imported package comes from.
Start here.
Python can import modules from its standard library without a separate package installation. Other features rely on third-party packages, which are installed into a particular Python environment. An environment is a Python interpreter together with the packages available to it. If a terminal installs a package for one interpreter while an editor runs another, the code may raise ModuleNotFoundError even though installation appeared to succeed.
A virtual environment is an isolated directory for one project's interpreter and packages. python -m venv .venv creates it; activating it changes which python command the shell selects. python -m pip install openpyxl then asks that selected interpreter to run its package installer. The -m form helps keep pip paired with the Python you intend to use. The activation command varies by shell and operating system, so the example shows one common POSIX form and explains the Windows equivalent.
A package's installation name may differ from its import name: Pillow is imported as PIL, and python-docx as docx. Some packages also need a separate operating-system program, such as Tesseract for OCR. A dependency list records what a project needs so another person can recreate the environment. The generated .venv directory is local machinery, while source code and dependency declarations describe the project itself.
Build the model.
Python includes a standard library, but chapters on spreadsheets, images, and speech also use third-party packages. A package is installed into a particular Python environment. If pip installs into one interpreter while your editor runs another, import will fail even though installation appeared successful. The reliable question is: which Python will execute this project, and which packages are installed for that Python?
Create a virtual environment inside a project and activate it before installing dependencies. This keeps unrelated projects from accidentally sharing versions. The environment directory itself is generated and should normally be ignored by version control; commit a dependency list or project configuration so another person can recreate it.
How it works.
Run python -m venv .venv to create an environment, then activate it using the command for your shell. Run python -m pip install package-name so pip belongs to the same interpreter as python. Check python -m pip show package-name or print the module's __file__ when import location is confusing. Prefer the package's official installation guide for system dependencies such as Tesseract or a speech backend.
Pin dependency versions for repeatable work, but update them deliberately when security or compatibility requires it. A lock file records a resolved set when your package manager supports one. Do not copy an entire machine's package list into every small script; describe what the project actually needs. Keep secrets such as API keys outside dependency files and source control.
A package name and its import name can differ: python-docx is imported as docx, and Pillow is imported as PIL. Read the package documentation rather than guessing. If installation fails, inspect the error, interpreter version, platform, and any native-library prerequisite before trying random commands.
Read it line by line.
python -m venv .venv
. .venv/bin/activate
python -m pip install openpyxl
python -m pip show openpyxlThe first command creates an isolated Python environment. The second activates it in a POSIX shell such as bash; on Windows PowerShell, use .venv\Scripts\Activate.ps1 instead. The pip commands then install and inspect openpyxl in that environment. Run these in a terminal, not in a .py file.
See the rule again.
import sys
print(sys.executable)
print(sys.version.split()[0])The first line shows the executable path; the second shows the Python version. Compare that path with the environment where you installed packages. The exact output depends on your machine.
A mistake to avoid.
A ModuleNotFoundError after installation often means the script is using another Python interpreter. Compare the interpreter in your editor with python -c 'import sys; print(sys.executable)' in the terminal.