How to Run Python in Production
a year ago
- #Production
- #Python
- #MachineLearning
- Python is dominant for data analysis and machine learning in production.
- Recommended package managers: poetry and uv (uv is fast but not yet stable).
- Use linters like autoflake, flake8, ruff, isort, and pylint to catch errors in dynamically typed Python code.
- Prevent secret leaks with tools like gitguardian, gitleaks, or noseyparker.
- Use pre-commit hooks for enforcing code quality.
- For web services, FastAPI + gunicorn offers 3X throughput over Flask + gunicorn.
- Use data-classes or pydantic for data handling and helper classes for pure functions.
- Avoid Python's async and multi-threading due to GIL issues; use multiple processes instead.
- Use pip-audit and deptry for dependency management and cleanup.
- Ensure legal compliance with licensecheck to avoid problematic library licenses.
- Deploy with Docker, avoiding Alpine-based images due to glibc vs musl issues.
- Use CPU-only PyTorch for non-GPU deployments to reduce size.
- Compile code during Docker builds for faster boot times.
- Download external dependencies at build time to avoid delays and ensure hermetic builds.
- Run Docker containers as non-root users to reduce attack surface.