How I Setup a Python Project from Scratch

Prerequisites
Before you begin, ensure you have:
- A Git repository
- A working Python environment
Setting Up Poetry
Install Poetry (if not already installed)
Use the following command to install Poetry:
|
|
๐ Reference: Poetry Installation Guide
Initialize Poetry in Your Project
If youโre setting up Poetry in an existing project:
|
|
๐ Reference: Initializing a Pre-Existing Project
Adding Linters
Use the --group dev
option to manage development tools as development dependencies.
Option 1: Traditional Approach
Commonly Used Tools
black
: Code formattingisort
: Import sortingflake8
: Code style checking
|
|
Style Configuration
Create a configuration file:
|
|
Add the following configuration to setup.cfg
:
|
|
โ ๏ธ Note:
flake8
does not supportpyproject.toml
for configuration.
Option 2: Modern Approach (I Personally Recommend This Way)
Use ruff
, a fast all-in-one linter and formatter.
|
|
โ Widely adopted by many modern open-source Python projects.
Setting Up Pre-commit Hooks (Optional but Recommended)
pre-commit
helps enforce code style and quality checks before commits.
Install Pre-commit
|
|
Initialize Pre-commit in Your Repo
|
|
Create Configuration File
Create the configuration file at the root of your project:
|
|
Configuration for Option 1 (Traditional Linters)
|
|
Configuration for Option 2 (ruff
)
|
|
Using a Makefile for Developer Workflow
A Makefile
can help streamline common tasks like setting up the development environment, running tests, or starting services.
Create a Makefile
|
|
Add a Setup Command
Add the following to your Makefile
:
|
|
This command will install dependencies and set up the pre-commit hooks.
Conclusion
I personally have the demand to create a project from scratch when I write this article. -> github-repo
The article can also help me to on board a new developer If there is a need one day in the future.
Although it might be over simplified for a big project but it is a correct way to start a Python project.