Python-in-the-Shell
Virtual environments, uv, pipx, and running Python scripts on Linux - how operators and developers invoke Python without breaking system packages.
Search across all documentation pages
Virtual environments, uv, pipx, and running Python scripts on Linux - how operators and developers invoke Python without breaking system packages.
cd billing-api
uv sync
uv run pytest -q
uv run python -m billing.cli --help
pipx install ruffWhen to reach for this:
#!/usr/bin/env bash
set -euo pipefail
cd /opt/billing-api
export PATH="$HOME/.local/bin:$PATH"
# Deploy user uses locked env
uv sync --frozen --no-dev
uv run alembic upgrade head
uv run python -m billing.worker --once
# Global tool without project pollution
pipx ensurepath
pipx install 'httpie>=3.0'#!/usr/bin/env python3
"""Shebang script - prefer uv run in production."""
import sys
def main() -> int:
print("ok")
return 0
if __name__ == "__main__":
sys.exit(main())What this demonstrates:
uv sync --frozen matches CI lockfile exactly-m module respects package importspipx isolates CLI toolsbin/site-packages and interpreter symlinks.uv run auto-uses project venv.~/.local/bin.apt tools only - not app deps.| Pattern | Use |
|---|---|
uv run pytest | Project-bound commands |
uv tool run ruff | Ephemeral tool without install |
pipx run cowsay | One-shot CLI |
python -m http.server | Stdlib quick server (dev only) |
# Which python am I using?
uv run python -c "import sys; print(sys.executable)"
# PEP 723 script deps (uv)
# /// script
# requires-python = ">=3.14"
# dependencies = ["httpx"]
# ///sudo pip install - Breaks system package manager. Fix: venv, uv, or pipx only.uv run in crontab.env python3 + documented version pin in README..venv not in .gitignore - Accidental commit. Fix: gitignore + CI creates fresh venv.pipx upgrade-all quarterly.| Alternative | Use When | Don't Use When |
|---|---|---|
poetry run | Poetry-standardized repos | Team standardized on uv |
conda | Scientific stack with binary deps | Slim API containers |
docker run | Exact prod parity on laptop | Fast edit-test loop |
pyenv | Multiple Python versions locally | Container pins single version |
This cookbook pins uv 0.6+ - use uv sync and uv.lock unless org ADR says otherwise.
~/.local/pipx/venvs with shims in ~/.local/bin - ensure PATH in shell profile.
ExecStart=/opt/billing-api/.venv/bin/uvicorn app.main:app or uv run wrapper script.
Debian/Ubuntu block system pip - use uv venv, expected on modern distros.
uv run script.py with inline metadata deps (PEP 723).
uv sync with editable local package in pyproject.toml - see environments-packaging doc.
Separate /opt/<app> dirs each with own .venv - never shared site-packages.
Use uv python install 3.14 or official deadsnakes PPA per org policy.
Redirect to logger: ... 2>&1 | logger -t billing-worker.
pipx inject package dependency==x.y when tool needs plugin.
Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.
Reviewed by Chris St. John·Last updated Jul 16, 2026