Purpose
Separate standard-library, third-party and local imports, pin declared dependencies in a project venv and verify the interpreter from a clean session. The Python For Data Science Cheat Sheet groups libraries by purpose and shows common import patterns, including importing a library, assigning an alias and importing a selected object. This guide applies those conventions to a maintainable analytics workflow.
1. Separate the Python standard library from third-party packages
The standard library is distributed with Python. Third-party packages must be installed into the environment used to run the analysis. Keep these categories distinguishable in project documentation and dependency files.
A clean setup reduces the risk that a script runs on one laptop but fails on another because a package was installed globally, installed in a different environment or never declared.
2. Use conventional import patterns
Import a package under its conventional alias when the ecosystem has a widely used convention:
import numpy as np
import pandas as pdA selective import can be useful for a small, stable object:
from math import piAvoid importing many names with from package import *. It makes it difficult to identify where a function came from and can create name collisions.
3. Group imports consistently
A readable script normally groups:
- standard-library imports;
- third-party packages;
- project-local modules.
Keep one import per line where practical, remove unused imports and avoid changing aliases throughout a project. Consistency helps reviewers and makes code search more reliable.
4. Match libraries to analytical responsibilities
The cheat sheet identifies NumPy with numerical and array-oriented work and points to plotting libraries for visualization. In a typical workflow:
- use NumPy for numerical arrays and mathematical operations;
- use pandas for labeled tabular data and data preparation;
- use a plotting library for charts;
- use specialized packages only when a defined analytical requirement needs them.
Do not import a large stack automatically. Begin with the task and add dependencies as the workflow requires them.
5. Make the environment reproducible
Record the Python version and direct package dependencies. Use a project-specific virtual environment rather than relying on a shared global installation. Pin versions when reproducibility or deployment stability requires it, and update them deliberately after testing.
A requirements file should list dependencies actually used by the project. It should not become a copy of every package installed on the analyst's machine.
6. Verify imports early
At the beginning of a new project, run a minimal environment check:
- Python executable is the expected one;
- imports succeed;
- package versions are recorded;
- notebook kernel or IDE uses the same environment;
- the script can be run from a clean session.
An import failure is an environment issue until evidence points elsewhere. Check the interpreter and package installation target before rewriting working analysis code.
7. Keep notebooks and scripts maintainable
Notebooks are useful for exploration, but move reusable transformations into functions or modules when a workflow becomes repeated. Keep import cells near the top, avoid hidden state from out-of-order cell execution and restart the kernel to confirm the notebook runs from a clean state.
Checklist
Declare dependencies, use conventional aliases, avoid wildcard imports, separate environment setup from analysis logic, and verify the interpreter used by the notebook or script. Reproducibility begins before the first chart: it begins with knowing exactly which code and libraries produced the result.
Official references
- Modules Checked November 23, 2026
- Creation of virtual environments (venv) Checked November 23, 2026
What changed
- v1Initial bilingual publication