Virtual environments
This page creates your course project folder, gives it its own environment and installs every library the course uses.
What a virtual environment is
Section titled “What a virtual environment is”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.
Create the project folder
Section titled “Create the project folder”Make a folder called my-course. You keep all your course scripts and the course data in it.
mkdir my-coursecd my-coursemkdir makes a directory (a folder). cd changes into it. From now on, run every command from inside my-course.
Create the environment with uv
Section titled “Create the environment with uv”uv venv --python 3.13uv 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.
Activate the environment
Section titled “Activate the environment”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.
source .venv/bin/activate.venv\Scripts\activateIf PowerShell says that running scripts is disabled, allow scripts for your user once, then activate again:
Set-ExecutionPolicy -Scope CurrentUser RemoteSignedsource .venv/bin/activateAfter 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.
Create requirements.txt
Section titled “Create requirements.txt”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.
# 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/cpunumpy==2.5.4pandas==3.1.1polars==2.1.0pyarrow==26.0.1openpyxl==3.1.5matplotlib==3.11.2seaborn==0.13.2plotly==7.1.0scipy==1.18.1statsmodels==0.15.0scikit-learn==1.9.1xgboost==3.4.1lightgbm==4.7.0optuna==5.0.0imbalanced-learn==0.14.2umap-learn==0.5.12shap==0.54.1torch==2.15.0fastapi==0.162.2httpx==0.28.1pydantic==2.14.3mlflow==3.18.0duckdb==2.0.0requests==2.34.2beautifulsoup4==4.15.0pytest==9.2.0pytest-cov==7.1.0ruff==0.17.4mypy==2.4.1joblib==1.6.0tabulate==0.10.0pyyaml==6.0.3Lines 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.
Install the course libraries
Section titled “Install the course libraries”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.
uv pip install -r requirements.txtRemember the libomp step for XGBoost and LightGBM.
uv pip install torch==2.15.0 --index-url https://download.pytorch.org/whl/cpuuv pip install -r requirements.txtuv pip install torch==2.15.0 --index-url https://download.pytorch.org/whl/cpuuv pip install -r requirements.txtuv 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.
Check the install
Section titled “Check the install”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.
import numpyimport pandasimport polarsimport sklearn
print("numpy", numpy.__version__)print("pandas", pandas.__version__)print("polars", polars.__version__)print("scikit-learn", sklearn.__version__)numpy 2.5.4pandas 3.1.1polars 2.1.0scikit-learn 1.9.1If 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.
Alternative: venv and pip
Section titled “Alternative: venv and pip”venv and pip come with Python. They do the same job as uv, only more slowly. Use them if you cannot install uv.
-
Create the environment. On Windows, write
pyinstead ofpython3.Terminal python3 -m venv .venv -
Activate it exactly as shown in Activate the environment.
-
Update pip, the standard package installer:
Terminal python -m pip install --upgrade pip -
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 -
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.
Next step
Section titled “Next step”Set up an editor that knows about your environment: Your editor.