Skip to content

Virtual environments

This page creates your course project folder, gives it its own environment and installs every library the course uses.

A virtual environment is a folder that holds one Python interpreter and its own set of installed packages. Each project gets its own environment. The convention is to call the folder .venv and to keep it inside the project folder.

Why not install everything into the system Python?

  • Projects need different versions. One project needs pandas 2, another needs pandas 3. Two environments can hold both.
  • You cannot break your system. Some operating systems use their own Python for their tools. Packages you install in an environment never touch it.
  • Others can repeat your setup. A list of exact versions recreates the same environment on any computer.

That list is a requirements file, usually called requirements.txt. Each line names a package and, ideally, its exact version, for example pandas==3.1.1. A version written with == is pinned: the installer always picks exactly that version. The course pins every version, so your output matches the output on this site.

Make a folder called my-course. You keep all your course scripts and the course data in it.

Terminal
mkdir my-course
cd my-course

mkdir makes a directory (a folder). cd changes into it. From now on, run every command from inside my-course.

Terminal
uv venv --python 3.13

uv creates the folder .venv with Python 3.13 inside it. If that version is not on your computer, uv downloads it. You can write --python 3.12 instead; any version from 3.12 works.

To activate an environment means to tell this terminal window to use the Python inside .venv. Do it each time you open a new terminal for the course.

Terminal
source .venv/bin/activate

After activation, the terminal prompt starts with (my-course) or (.venv). The command python now runs the environment’s Python on every system. To leave the environment, type deactivate.

In my-course, create a new file called requirements.txt. Copy the block below into it exactly as it is and save it. The copy button is in the top-right corner of the block.

requirements.txt
# Pinned course dependencies. Python 3.12+.
# Install: uv pip install -r python/requirements.txt
# PyTorch: macOS wheels are CPU-only. On Linux/Windows install the CPU wheel first:
# uv pip install torch==2.15.0 --index-url https://download.pytorch.org/whl/cpu
numpy==2.5.4
pandas==3.1.1
polars==2.1.0
pyarrow==26.0.1
openpyxl==3.1.5
matplotlib==3.11.2
seaborn==0.13.2
plotly==7.1.0
scipy==1.18.1
statsmodels==0.15.0
scikit-learn==1.9.1
xgboost==3.4.1
lightgbm==4.7.0
optuna==5.0.0
imbalanced-learn==0.14.2
umap-learn==0.5.12
shap==0.54.1
torch==2.15.0
fastapi==0.162.2
httpx==0.28.1
pydantic==2.14.3
mlflow==3.18.0
duckdb==2.0.0
requests==2.34.2
beautifulsoup4==4.15.0
pytest==9.2.0
pytest-cov==7.1.0
ruff==0.17.4
mypy==2.4.1
joblib==1.6.0
tabulate==0.10.0
pyyaml==6.0.3

Lines that start with # are comments; the installer skips them. The comment mentions python/requirements.txt, which is where the file lives in the course’s own source code. In your project, the file is requirements.txt in my-course.

The download is large, because the list includes the full data science and machine learning stack. PyTorch is the biggest part. The install can take several minutes the first time.

PyTorch is the deep learning library of module 42. The course uses its CPU build. The CPU is the main processor of every computer; the CPU build does not need a graphics card. On macOS, the standard PyTorch package is already the CPU build. On Windows and Linux, the standard package includes large extra parts for graphics cards, so install the CPU build first.

Terminal
uv pip install -r requirements.txt

Remember the libomp step for XGBoost and LightGBM.

uv pip install installs into the .venv folder of the current project. -r requirements.txt means “read the package list from this file”. --index-url tells uv to download PyTorch from the PyTorch site instead of the standard package index.

Run this check once your editor is set up (see Your editor), or paste it into a file and run python check_install.py. Each library has a __version__ attribute: its version as text.

check_install.py
import numpy
import pandas
import polars
import sklearn
print("numpy", numpy.__version__)
print("pandas", pandas.__version__)
print("polars", polars.__version__)
print("scikit-learn", sklearn.__version__)
Output
numpy 2.5.4
pandas 3.1.1
polars 2.1.0
scikit-learn 1.9.1

If an import fails with ModuleNotFoundError, the environment is not active, or the install did not finish. Activate the environment and run the install command again.

venv and pip come with Python. They do the same job as uv, only more slowly. Use them if you cannot install uv.

  1. Create the environment. On Windows, write py instead of python3.

    Terminal
    python3 -m venv .venv
  2. Activate it exactly as shown in Activate the environment.

  3. Update pip, the standard package installer:

    Terminal
    python -m pip install --upgrade pip
  4. On Windows and Linux only, install the PyTorch CPU build first:

    Terminal
    python -m pip install torch==2.15.0 --index-url https://download.pytorch.org/whl/cpu
  5. Install the course libraries:

    Terminal
    python -m pip install -r requirements.txt

python -m pip runs pip with the Python of the active environment. It is safer than a bare pip command, which can belong to another Python.

Set up an editor that knows about your environment: Your editor.