No matter what kind of project you plan to do (Django/Flask web app or pure-python CLI tool) the Python project template brings you the best practices in code style, code quality, and automation.
Enjoy the development of your new project 🏖️
- Automated with
make
development environment:- Creating virtualenv
- Installing requirements
- Code-style checking with
ruff
- Linting with
ruff-format
- Unit testing
- Coverage checking
- Docker Compose up/down
- Running the app
- Cleaning up workdir
- GitHub Actions workflow
- Dockerfile & docker-compose
This project template is using rye as a python project manager. There are several advantages to this as rye will act as package manager and also manage virtual environment completely decoupled from your local setup. This means, as long as you have rye installed, it will completely manage the project locally.
To install rye on your computer, follow the guide.
- Create a new Repository with this as repo as template
- Clone the repo
- Run
make init
- run
rye sync
The make init
-command sets up the folders for sources and creates a pyproject.toml, internally it uses rye init --py 3.12
to add Python 3.12.x as required. The command rye sync
creates a virtual environment in a .venv locally and install all the packages as specified in the pyprojects.toml.
To add dependencies use rye add
and rye sync
after wards. To add development dependencies use the command rye add -- dev <package name>
. When you run rye sync
it automatically downloads and install all packages, including development requirements. To skip installation of development dependencies, use rye sync --no-dev
.
There are two ways to import local modules, either absolute or relative:
- Absolute import works for a built package as well as when running the script locally. It might raise some Pylance errors during use.
- Relative will not work when running the file locally, but works good when installed as a package and with various type-checking tools
Example of Absolute import:
from toolsense.erpnext.api import api
Example of relative import:
from .api import api
If relative imports are used, it is also possible to create a main.py in the project folder and use absolute import is this file. That will allow the relative imports inside the package to work.
It is possible to use this this template to create a new package in the Toolsense namespace. It is however important to do the following:
- Create a new repo using this template. It should be named
toolsense-<package name>
. - Clone and enter the project folder.
- Create a new folder in src/ called "toolsense".
- Remove the toolsense prefix from your package name.
- Move your package files to this folder.
- Change the build targets in the pyproject.toml to
packages = ["src/toolsense"]
.
The final structure will look like the below for the project toolsense-erpnext:
toolsense-erpnext
├── docker-compose.yml
├── Dockerfile
├── entrypoint.sh
├── Makefile
├── pyproject.toml
├── README.md
├── requirements-dev.lock
├── requirements.lock
├── ruff.toml
└── src
└── toolsense
└── erpnext
└── __init__.py
The naming of the repo is typically a few words separated by a dash (-) and the package name is the same name but separated by underscores (_). It is not good practice to have a repository or package name with a _test
-suffix as this is used for test-cases within the package.
To get started with this template:
- When creating a new repo, use this as a template in Github.
- Clone your repo
- Rename the python_project_template-folder to match the name of your repo
- Change the name in pyproject.toml to match the name of the "project", i.e. the folder or name of the repo.
- Make adaptions according to your need
- Makefile
- Docker & Docker-Compose
- Add dependencies
- Have fun!
- Run
make
for the help message.
For example make check
will do:
- create virtualenv (if it doesn't created yet)
- install requirements (if it doesn't installed yet)
- install dev requirements (if it doesn't installed yet)
- run
ruff format --check --diff .
- run
ruff check .
- run
mypy
- run unit tests
GitHub Actions use the same (make check
) command to check pull requests.
- Pre-commit hook