Virtuelle miljøer er hjørnesteinen i Python-avhengighetsadministrasjon. Denne guiden dekker de fire hovedverktøyene: venv, conda, pipenv og Poetry.
Hvorfor virtuelle miljøer er viktige
Hvert Python-prosjekt har egne avhengigheter med spesifikke versjonskrav. Virtuelle miljøer løser dette ved å lage isolerte Python-installasjoner per prosjekt.
venv: Innebygd standardbibliotek
venv er inkludert med Python 3.3+ og er det enkleste alternativet.
# 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: Moderne avhengighetsadministrasjon
Poetry er den mest moderne Python-avhengighetsbehandleren.
# 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-kraftverk
conda er en fullstendig pakkebehandler som kan installere ikke-Python-avhengigheter.
# 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.0Verktøysammenligning
Valget av riktig verktøy avhenger av brukstilfellet.
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)Vanlige spørsmål
Hvilket verktøy bør jeg bruke i 2026?
For generell Python-utvikling, bruk Poetry. For data science, bruk conda.
Forskjell mellom pip og conda?
pip installerer bare fra PyPI. conda kan installere ikke-Python-pakker.
Kan jeg bruke pip i et conda-miljø?
Ja, men med forsiktighet.
Hvordan dele miljøet med andre?
Med Poetry: pyproject.toml og poetry.lock. Med pip: requirements.txt. Med conda: environment.yml.