Python environments

Definition

A Python environment is a runtime context that includes a Python interpreter and a set of installed dependencies. The idea is to isolate projects so you avoid version conflicts between libraries.

Running Python code

Global interpreter

This is the Python installed at the system level (python, python3). It is very simple—no need to manage environments—but it is really only suited to small one-off scripts and simple exploratory notebooks, because dependency issues show up quickly over time. For my own work, whether scripts or notebooks, I use at least a .venv virtual environment, optionally with a minimal requirements.txt, especially since creating and managing one is not hard.

Virtual environment (.venv)

venv is a built-in Python module that creates isolated virtual environments for different projects. It is a bit like having a separate Python install per project.

Chargement…

You can also create a .venv directly from VS Code or Cursor (what I do most often):

  1. Open the project folder at the repo root.
  2. Ctrl+Shift+P (command palette).
  3. Choose Python: Create Environment (sometimes labeled Create Virtual Environment).
  4. Select Venv, then the Python interpreter you want.

The editor creates the .venv folder and usually offers to activate that environment for the workspace.

Note: For Jupyter notebooks, the environment alone is not enough. Install ipykernel (and often jupyter) in the .venv, then select that kernel in the notebook (top-right corner, or the Select Kernel command).

Chargement…

Conda / Mamba environment

Conda/Mamba manage both Python and non-Python dependencies (C/C++, MKL, CUDA, etc.). Very useful when you want to train a machine learning model such as Random Forest or XGBoost on the GPU rather than the CPU with cuML (CUDA) instead of scikit-learn.

Chargement…

This is what I recommend for projects you expect to scale.

Bonus: Micromamba

On my Linux machine I use Micromamba, a minimal Mamba build shipped as a single executable—no Anaconda install required. On Windows I stick with .venv and pip.

Chargement…

Installing packages

Once the environment is active (or selected in the editor), you install libraries: numpy, pandas, fastapi, and so on.

pip

pip downloads packages from an index (often PyPI) into the current environment. It is the most common installer with venv.

Chargement…

What I usually do on a small project: activate .venv → pip install -r requirements.txt.

conda / mamba

In a conda environment, install with conda install (conda binaries) or sometimes pip for packages missing on conda-forge.

Chargement…

Handy when you need heavy libs (MKL, CUDA, etc.) without compiling everything yourself.

poetry

Poetry manages dependencies and lock files from pyproject.toml.

Chargement…

Useful when the project is already structured around Poetry (team workflow, packaging, releases).

uv

A modern, very fast alternative, often used as an accelerated pip/poetry workflow.

Chargement…

Dependency files

requirements.txt

The historical format for pip.

  • easy to read,
  • widely used,
  • often produced with pip freeze (watch out: it can include too many transitive dependencies).
Chargement…

environment.yml / environment.yaml

Conda/Mamba format: Python version + conda packages + optional pip packages.

Chargement…

pyproject.toml

Modern standard format (PEP 621): project metadata, dependencies, and tool configuration (poetry, hatch, ruff, etc.).

Chargement…

Lock files

They pin exact versions for reproducible installs.

Examples:

  • poetry.lock
  • pdm.lock
  • uv.lock
  • conda-lock.yml

Without a lock file, two machines can end up with different versions.

Concrete examples

Work in progress

Summary

Work in progress

References

Work in progress

https://docs.python.org/3/library/venv.html https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#create-and-use-virtual-environments https://packaging.python.org/en/latest/key_projects/#pip https://docs.python.org/3/installing/index.html https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html