Python Tutorial (7): Virtual Environment dan Dependency Management
venv, pip, requirements.txt, Poetry, pyproject.toml, dan strategi reproducible environment untuk project Python.
Setiap project Python harus memiliki environment terisolasi. Dependency project A tidak boleh bertabrakan dengan project B. Tutorial ini membahas tools dan best practice dependency management.
Mengapa Virtual Environment?
Tanpa isolasi:
System Python:
requests==2.25 ← project A butuh ini
requests==2.31 ← project B butuh ini
❌ Konflik!Dengan venv:
project-a/.venv → requests==2.25 ✅
project-b/.venv → requests==2.31 ✅venv: Built-in Solution
# Buat virtual environment
python3 -m venv .venv
# Aktivasi
source .venv/bin/activate # Linux/macOS
# .venv\Scripts\activate # Windows
# Verifikasi
which python # .venv/bin/python
python --version
pip --version
# Deaktivasi
deactivateConvention: nama .venv atau venv, tambahkan ke .gitignore.
pip dan requirements.txt
# Install package
pip install requests flask sqlalchemy
# Install versi spesifik
pip install "requests>=2.28,<3.0"
pip install "django~=4.2" # compatible release (4.2.x)
# Export dependency
pip freeze > requirements.txt
# Install dari file
pip install -r requirements.txt
# Upgrade
pip install --upgrade requests
pip install --upgrade piprequirements.txt Best Practice
# requirements.txt: pin exact versions untuk reproducibility
requests==2.31.0
flask==3.0.0
sqlalchemy==2.0.23
gunicorn==21.2.0# requirements-dev.txt: development dependencies
-r requirements.txt
pytest==7.4.3
ruff==0.1.8
mypy==1.7.1pip-tools: Lebih Baik dari Freeze
pip install pip-tools
# requirements.in: deklarasi top-level dependency
# requirements.txt: generated, pinned, resolved# requirements.in
flask
sqlalchemy
celerypip-compile requirements.in # → requirements.txt with hashes
pip-sync requirements.txt # sync environment exactlyPoetry: Modern Dependency Management
Poetry menggabungkan venv management, dependency resolution, dan packaging.
# Install Poetry
curl -sSL https://install.python-poetry.org | python3 -
# Buat project baru
poetry new myproject
# Atau init di project existing
cd myproject
poetry init
# Install dependency
poetry add requests flask
poetry add --group dev pytest ruff
# Install semua dari lock file
poetry install
# Run di venv Poetry
poetry run python main.py
poetry run pytest
# Shell aktivasi
poetry shellpyproject.toml
[tool.poetry]
name = "myproject"
version = "0.1.0"
description = "My awesome project"
authors = ["Alice <alice@mail.com>"]
python = "^3.11"
[tool.poetry.dependencies]
python = "^3.11"
flask = "^3.0"
sqlalchemy = "^2.0"
pydantic = "^2.5"
[tool.poetry.group.dev.dependencies]
pytest = "^7.4"
ruff = "^0.1"
mypy = "^1.7"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"poetry.lock
File lock = snapshot exact version semua dependency (termasuk transitive). COMMIT file ini ke git untuk reproducibility.
uv: Blazing Fast Alternative
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# Buat project
uv init myproject && cd myproject
# Add dependencies
uv add requests flask
uv add --dev pytest ruff
# Sync environment
uv sync
# Run
uv run python main.py
uv run pytestuv sangat cepat (10-100x pip) dan compatible dengan pip ecosystem.
pyproject.toml: Standard Modern
Semua tools modern converge ke pyproject.toml (PEP 621):
[project]
name = "myproject"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"flask>=3.0",
"sqlalchemy>=2.0",
]
[project.optional-dependencies]
dev = [
"pytest>=7.4",
"ruff>=0.1",
]
[tool.ruff]
line-length = 100
[tool.pytest.ini_options]
testpaths = ["tests"]Strategi Project Structure
myproject/
├── pyproject.toml # project metadata + dependencies
├── poetry.lock / uv.lock # lock file (commit!)
├── .python-version # Python version (pyenv)
├── .gitignore
├── README.md
├── src/
│ └── myproject/
│ ├── __init__.py
│ ├── main.py
│ └── utils.py
├── tests/
│ ├── __init__.py
│ └── test_main.py
└── .venv/ # JANGAN commit.gitignore:
.venv/
__pycache__/
*.pyc
dist/
*.egg-info/
.envMultiple Python Versions: pyenv
# Install pyenv
curl https://pyenv.run | bash
# Install Python version
pyenv install 3.12.1
pyenv install 3.11.7
# Set per-project
cd myproject
pyenv local 3.12.1 # buat .python-version
# Global default
pyenv global 3.12.1Latihan Praktis
- Buat project dengan venv, install 3 package, export requirements.txt
- Setup project baru dengan Poetry atau uv, tambahkan dev dependencies
- Buat
.gitignoreyang tepat untuk Python project - Coba lock file: install di mesin lain, verifikasi versi identik
Rangkuman
Dependency management yang benar = reproducible builds. Gunakan venv minimal, Poetry/uv untuk project serius. Selalu pin versions dan commit lock file. Tutorial selanjutnya: iterators, generators, dan decorators.