A personal, opinionated guide to managing Python projects. Literature that extends the argumentation and explains it on a more detailed level:
- Tooling
- Dockerfile Optimizations
- CUDA Development Workbenches
- Standard Project Structure
- Mono-Repository Structure
- Workflows
- Use pyenv to manage
Python
versions. - Use pipx to install and run global
Python
applications in isolated environments (poetry
for example). - Use poetry to manage
Python
dependencies and packaging. - [minor] use ruff as a linter/formatter.
This guide recommends the following techniques:
- Multi-stage builds:
-
- To parallelize builds to increase speed.
-
- To separate build and runtime stages to reduce final image size.
- Effective cache utilization to speed-up build times by:
-
- Positioning expensive layers early.
-
- Placing frequently changing layers last.
-
- Keeping layers small (including only necessary files and dependencies).
-
- Minimizing layer count.
Examples could be found in both, standard and monorepo structures.
This guide also covers how to build comprehensive CUDA environment workbenches, that can reliably build deep learning frameworks, such as PyTorch, from source. The same Dockerfile file can be found in standard and monorepo structures.
For details on the standard project structure, refer to the standard structure documentation.
For details on the mono-repository structure, refer to the mono-repository structure documentation.
Follow these steps to start a new project:
pyenv install 3.12.3 # or the version you need
pyenv local 3.12.3
poetry init
... # Configure as needed
poetry install --no-root
Follow these steps to install an existing project:
pyenv install 3.12.3 # or the version you need
pyenv local 3.12.3
poetry install
Follow these steps for local development:
poetry run ruff format # format the files
poetry run ruff check --fix # apply linting fixes
poetry run python -m unittest discover
poetry run mypy .
Follow these steps to build Docker images for services:
# Inside the service directory
export IMAGE_TAG=python-service-x
docker build -f Dockerfile -t $IMAGE_TAG .
Follow these steps to run Docker containers locally:
# Be sure to have built the Docker image before
export IMAGE_TAG=python-service-x
docker run -p 8080:8080 --name local $IMAGE_TAG
For details on CI/CD workflows, refer to the respective documentation: