Introduction to Python and Conda

Master 2 (203) in Financial Markets — Paris Dauphine | PSL

Juan F. Imbet

2025-09-16

Agenda

  • Quick Python history and ecosystem landscape
  • Why manage Python with Conda (vs. system Python)
  • Install options: Miniconda vs. Anaconda vs. Mambaforge
  • Environments: create, activate, export, reproduce
  • Package management: conda and pip together (best practices)
  • Alternatives: pip + venv/virtualenv and when to choose them

Python: a brief history

  • 1991: Python 0.9 by Guido van Rossum
  • 2000: Python 2.0 (list comprehensions, GC); 2008: Python 3.0 (unicode, cleanups)
  • 2020: EOL for Python 2 — modern stack is Python 3.x
  • Strengths: readability, rich ecosystem (data, web, ML, finance)
  • Governance: PEPs, Steering Council; releases every ~12 months (3.12/3.13)

Why Python for finance/data

  • Clear syntax lowers cognitive load; fast to prototype
  • Massive libraries: NumPy, pandas, SciPy, scikit-learn, statsmodels
  • Interop: C/Fortran, Rust, Java, SQL, cloud SDKs
  • Community + tooling: Jupyter, VS Code, CI/CD, packaging
  • Strong adoption in quant/market data/analytics

Why Conda (big picture)

  • Solves “dependency hell”: binary packages for C/Fortran libs (e.g., NumPy)
  • Environments isolate projects (Python versions and packages)
  • Cross-platform: Windows, macOS, Linux — reproducibility
  • Works with both conda and pip packages (hybrid installs)
  • Channels (defaults, conda-forge) provide curated, prebuilt wheels/conda-pkgs

Conda flavors and choices

  • Anaconda: full suite of packages; big download; great offline start
  • Miniconda: minimal installer; add only what you need
  • Mambaforge/Miniforge: conda-forge-first with fast solver (mamba)
  • Codespaces: often preinstalled; check conda --version

Install Conda (Windows/macOS/Linux)

  • Download Miniconda: https://docs.conda.io/en/latest/miniconda.html
  • On Windows: Run the .exe, “Just Me”, add to PATH optional (Conda init recommended)
  • Initialize shell: conda init then restart terminal (cmd/PowerShell)
  • Verify: conda --version and conda info
  • Optional: install mamba for speed: conda install -n base -c conda-forge mamba

Configure Conda defaults

  • A Conda default is a url to a channel (repo) of packages
  • Prefer conda-forge: conda config --add channels conda-forge
  • Make it top priority: conda config --set channel_priority strict
  • Show config file: conda config --show-sources
  • Where is it? Windows: %USERPROFILE%/.condarc, macOS/Linux: ~/.condarc
  • Export for portability: include channels in environment.yml

Create and manage environments

  • Create: conda create -n my_env311 python=3.11 numpy pandas -y
  • Activate/deactivate: conda activate my_env311 / conda deactivate
  • List envs: conda env list (or conda info --envs)
  • Remove: conda remove -n my_env311 --all
  • Update Python: conda install -n my_env311 python=3.12

Installing packages (conda)

  • Search: conda search <package>; info: conda info <package>
  • Install: conda install -n fm203-py310 -c conda-forge scikit-learn statsmodels
  • Update: conda update --all (be cautious; snapshot first)
  • Pin versions: conda install pandas=2.2 numpy=1.26
  • Rollback using revisions: conda list --revisionsconda install --revision N

Mixing conda and pip (safely)

  • Prefer conda for compiled libs; pip for packages not on conda-forge
  • Order matters: conda first, then pip in the same env
  • Use pip inside the activated env
  • Record dependencies: pip freeze > requirements.txt and conda env export --from-history
  • Avoid pip install --upgrade pip breaking solver; use python -m pip install --upgrade pip

Reproducible environments

  • Export minimal spec: conda env export --from-history > environment.yml
  • Include pip section: conda env export > environment.full.yml
  • Recreate: conda env create -f environment.yml
  • Lock with conda-lock: generates platform-specific locks
  • In Teams: commit environment.yml and document activation

VS Code for Python (local)

  • Install extensions: Python, Jupyter, Pylance
  • Select interpreter: Command Palette → “Python: Select Interpreter”
  • Run and debug: use the #%% markers for cells to mimic Jupyter inside of the IDE.

pip + venv/virtualenv (alternative)

  • Create venv: python -m venv .venv and activate (Scripts\\activate on Windows)
  • Install: python -m pip install numpy pandas jupyterlab
  • Freeze deps: python -m pip freeze > requirements.txt
  • Recreate: python -m venv .venv && pip install -r requirements.txt
  • When to prefer: simple apps, pure-Python deps, slim projects.

virtualenvwrapper and tools

  • virtualenv is faster/more features than venv in some cases
  • pip-tools (pip-compile, pip-sync) for deterministic pins
  • uv and pipx for global tooling isolation
  • Poetry/uv/pdm: higher-level project managers (beyond this course)
  • Rule: one toolchain per project; avoid mixing managers

Common pitfalls and fixes

  • PATH issues after install: rerun conda init, restart shell
  • Conflicting channels: prefer conda-forge with strict priority
  • Broken env: export work, recreate fresh, avoid --force-reinstall
  • GPU packages: match CUDA version; prefer conda-forge builds
  • Windows specifics: use cmd/PowerShell; avoid system Python mix-ups

References

  • Conda docs: https://docs.conda.io/
  • conda-forge: https://conda-forge.org/
  • Jupyter: https://jupyter.org/
  • VS Code Python: https://code.visualstudio.com/docs/python/python-tutorial
  • Python: https://www.python.org/