Standard Operation Procedure for Python
This SOP outlines the procedures and best practices for Python development to ensure consistency, quality, and maintainability of code.
- Create (public) GitHub repo
- create
.gitignore
using gh collection of useful .gitignore templates
Download the latest version of Python from python.org.-> usepyenv
or devcontainer
- Use virtual environments to manage dependencies for different projects.
- Do not use
python -m venv
, more effective tools are avaliable.
- (install python version):
pyenv install 3.12
- Create a virtual environment:
pyenv virtualenv 3.12 myenv-3.12
- Set a local Python version (specific to a directory):
pyenv local myenv-3.12
- use pre-build Python image
- use devcontainer gh repo
- custom vscode e.g.
git-graph
- custom features e.g.
common-utils:2
and"configureZshAsDefaultShell": "true"
- Create a
requirements.txt
file to list project dependencies. - Install dependencies:
pip install -r requirements.txt
Organize your project files in a logical structure:
my_project/
├── .gitignore
├── README.md
├── requirements.txt
├── setup.py
├── src/
│ ├── __init__.py
│ ├── main.py
│ ├── module/
│ │ ├── __init__.py
│ │ └── module_code.py
├── tests/
│ ├── __init__.py
│ └── test_main.py
├── .devcontainer/
│ └── devcontainer.json
└── docs/
└── index.md
The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you’re saying rather than on how you’re saying it.
- Adhere to PEP 8, the Python style guide: PEP 8
Use tools likeflake8
orpylint
to check for compliance.- Use
ruff
as linter.
- Use descriptive names for variables, functions, and classes.
- Follow these conventions:
- Variables and functions:
snake_case
- Classes:
CamelCase
- Constants:
UPPER_CASE
- Variables and functions:
- Use docstrings for modules, classes, and functions.
- Follow PEP 257 for docstring conventions: PEP 257
- Example: use
Args:
instead ofParameters:
def add(a, b): """ Add two numbers. Args: # Parameters: a (int): The first number. b (int): The second number. Returns: int: The sum of the two numbers. Raises: ValueError: Description of the exception. """ return a + b
- Initialize a Git repository:
git init
- Create a
.gitignore
file to exclude unnecessary files.
- Use a branching strategy like Git Flow or feature branches.
- Example:
main
ormaster
for production code.develop
for development.- Feature branches:
feature/feature-name
- Write clear and concise commit messages.
- Follow this structure:
feat: Add new feature fix: Fix a bug docs: Update documentation (e.g. Update README with setup instructions) style: Improve code style (e.g., formatting) refactor: Refactor code (no functional changes) test: Add or update tests chore: Miscellaneous tasks (e.g., build process) update: Bump lodash version to 4.17.21
- use my logging_template
- for most usecases use setup from
app.py
withlogging_config.yaml
- for most usecases use setup from
- Use
unittest
orpytest
for testing. - Place tests in a
tests
directory.
- Aim for high test coverage.
- Use tools like
coverage.py
to measure coverage.