Virtuele omgevingen zijn de hoeksteen van Python-afhankelijkheidsbeheer. Deze gids behandelt de vier belangrijkste tools: venv, conda, pipenv en Poetry.
Waarom virtuele omgevingen belangrijk zijn
Elk Python-project heeft zijn eigen afhankelijkheden met specifieke versievereisten. Virtuele omgevingen lossen dit op door geïsoleerde Python-installaties per project te maken.
venv: Ingebouwde standaardbibliotheek
venv is inbegrepen bij Python 3.3+ en is de eenvoudigste optie.
# venv — Built-in (Python 3.3+)
# Create a virtual environment
python -m venv .venv
# Activate (macOS/Linux)
source .venv/bin/activate
# Activate (Windows)
.venv\Scripts\activate
# Install packages
pip install django requests
# Save dependencies
pip freeze > requirements.txt
# Install from requirements.txt
pip install -r requirements.txt
# Deactivate
deactivate
# Delete the environment
rm -rf .venv
# Recommended: add .venv to .gitignore
echo ".venv" >> .gitignorePoetry: Modern afhankelijkheidsbeheer
Poetry is de meest moderne Python-afhankelijkheidsmanager.
# Poetry — Modern Dependency Management (Recommended 2026)
# Install Poetry
curl -sSL https://install.python-poetry.org | python3 -
# Create a new project
poetry new my-project
cd my-project
# Add dependencies
poetry add django
poetry add --group dev pytest black ruff
# Install all dependencies (from poetry.lock)
poetry install
# Run commands in the virtual environment
poetry run python manage.py runserver
poetry run pytest
# Open a shell in the virtual environment
poetry shell
# Update dependencies
poetry update
# Export to requirements.txt (for compatibility)
poetry export -f requirements.txt --output requirements.txt
# Build and publish a package
poetry build
poetry publish
# Show dependency tree
poetry show --tree
# pyproject.toml (auto-generated)
# [tool.poetry]
# name = "my-project"
# version = "0.1.0"
# description = ""
# [tool.poetry.dependencies]
# python = "^>=3.11"
# django = "^>=5.0"
# [tool.poetry.group.dev.dependencies]
# pytest = "^>=8.0"conda: Data science krachtpatser
conda is een volledige pakketmanager die ook niet-Python-afhankelijkheden kan installeren.
# conda — Data Science / ML (Anaconda/Miniconda)
# Install Miniconda (minimal)
# https://docs.conda.io/projects/miniconda/
# Create environment with specific Python version
conda create -n myproject python=3.11
# Activate environment
conda activate myproject
# Install packages (conda packages first)
conda install numpy pandas scikit-learn matplotlib
# Install packages not in conda
pip install some-pytorch-extension
# Export environment
conda env export > environment.yml
# Create from environment.yml
conda env create -f environment.yml
# List environments
conda env list
# Deactivate
conda deactivate
# Remove environment
conda env remove -n myproject
# Update conda
conda update conda
# environment.yml example:
# name: myproject
# channels:
# - conda-forge
# - defaults
# dependencies:
# - python=3.11
# - numpy=1.26
# - pandas=2.1
# - pip:
# - custom-package==1.0Toolvergelijking
De keuze van het juiste tool hangt af van uw gebruik.
Tool Installation Lockfile Non-Python Build/Publish Best For
------------------------------------------------------------------------
venv Built-in No No No Simple scripts, learning
pipenv pip install Yes No No Legacy projects
Poetry curl install Yes No Yes General apps, libraries
conda Installer Yes Yes No Data science, ML, AI
uv cargo/pip Yes No Yes Fast pip replacement (2026)Veelgestelde vragen
Welk tool te gebruiken in 2026?
Voor algemene Python-ontwikkeling gebruik Poetry. Voor data science gebruik conda.
Verschil tussen pip en conda?
pip installeert alleen van PyPI. conda kan ook niet-Python-pakketten installeren.
Kan ik pip gebruiken in een conda-omgeving?
Ja, maar met voorzichtigheid.
Hoe deel ik mijn omgeving?
Met Poetry: pyproject.toml en poetry.lock. Met pip: requirements.txt. Met conda: environment.yml.