Setting Up the Environment

Why Environment Management Matters

Scientific Python code depends on dozens of packages with complex interdependencies — NumPy, SciPy, PyTorch, CuPy, Matplotlib, and more. A single version mismatch can produce silent numerical errors or outright crashes. Proper environment management is not overhead; it is the foundation on which reproducible research rests.

This section sets up the toolchain you will use for the rest of the book.

Definition:

Virtual Environment

A virtual environment is an isolated directory tree containing a Python interpreter and a set of installed packages. Activating a virtual environment redirects python and pip to use this isolated tree, so that packages installed in one project do not interfere with another.

Python provides venv in the standard library:

python -m venv myenv
source myenv/bin/activate    # macOS/Linux
myenv\Scripts\activate       # Windows

Definition:

Conda Environment

Conda is a cross-platform package and environment manager that handles both Python packages and non-Python dependencies (C libraries, CUDA toolkit, MKL). A conda environment is created with:

conda create -n myproject python=3.11 numpy scipy matplotlib
conda activate myproject

Conda resolves binary compatibility automatically, which makes it the preferred tool for scientific computing stacks that include GPU libraries (CuPy, PyTorch with CUDA).

virtual environment

An isolated Python installation that keeps project dependencies separate from the system Python and from other projects.

Related: conda

conda

A package and environment manager that handles Python packages and non-Python dependencies (C/C++ libraries, CUDA). Distributed via Anaconda or Miniconda.

Related: virtual environment

Example: Creating a Reproducible Environment

Create a conda environment for this book with pinned dependencies, export it, and recreate it on another machine.

Environment Setup Script

python
A script that checks your Python version, verifies key packages are installed, and prints diagnostic information about your setup.
# Code from: ch01/python/setup_environment.py
# Load from backend supplements endpoint

Common Mistake: Never Use System Python for Scientific Work

Mistake:

Installing scientific packages directly into the system Python (sudo pip install numpy) or using the macOS/Linux system Python for project work.

Correction:

Always use a virtual environment or conda environment. System Python is managed by the OS and modifying it can break system tools. Use pyenv to manage multiple Python versions if needed:

pyenv install 3.11.8
pyenv local 3.11.8

Common Mistake: Mixing pip and conda Carelessly

Mistake:

Alternating between pip install and conda install in the same environment without a clear strategy, leading to dependency conflicts that are difficult to diagnose.

Correction:

Use conda to install packages that have complex binary dependencies (numpy, scipy, pytorch, cupy) and pip for pure-Python packages. Always install conda packages first, then pip packages. If using pip inside conda, prefer pip install over conda install for the same package — never install the same package with both.

venv vs. conda

Featurevenvconda
ScopePython packages onlyPython + C/C++/CUDA libraries
SpeedFast (no solver)Slower (SAT solver for deps)
Reproducibilityrequirements.txtenvironment.yml
GPU supportManual CUDA setupAutomatic CUDA toolkit
Disk usageLightweightHeavier (copies of libraries)
Best forPure Python projectsScientific computing stacks

Python Version Feature Timeline

Explore which Python features became available in each version. This helps you understand minimum version requirements for modern Python idioms used throughout the book.

Parameters
3.8

pyenv

A tool for managing multiple Python versions on a single machine. Allows switching between Python versions per-project via .python-version files.

Related: virtual environment

pip

Python's default package installer. Installs packages from the Python Package Index (PyPI). Command: pip install package_name.

Key Takeaway

Every project gets its own environment. Whether you use venv or conda, the rule is the same: never install scientific packages into the system Python, and pin your dependencies for reproducibility.

Quick Check

Which command creates a conda environment with Python 3.11 and NumPy?

conda create -n myenv python=3.11 numpy

pip install python=3.11 numpy

python -m venv myenv --with numpy

conda install python=3.11 numpy