A basic Python starter project with configurations for code formatting, linting, and type checking.
This project is a fork of pythonstarter.
- Type Checking: Using
mypyfor static type checking - Code Formatting: Using
blackandisortfor consistent code style - Linting: Using
rufffor fast Python linting - IDE Integration: Pre-configured settings for VS Code, PyCharm, and Emacs
- Git Integration: Pre-commit hooks to enforce code quality
- Dependency Management: Using Poetry for reliable dependency management
- Python 3.8 or newer
- Poetry (Python package and dependency manager)
-
Clone this repository:
git clone https://github.com/yourusername/python-starter.git cd python-starter -
Install Poetry (if not already installed):
# On macOS/Linux curl -sSL https://install.python-poetry.org | python3 - # On Windows (PowerShell) (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -
-
Configure Poetry to store virtual environments in the project directory (optional but recommended):
poetry config virtualenvs.in-project true -
Install project dependencies (including development tools):
poetry install
-
Activate the virtual environment (optional but useful):
# Find the path to the virtualenv poetry env info --path # Activate it manually (Linux/macOS) source .venv/bin/activate # On Windows (cmd) .venv\Scripts\activate
Once activated, you can use
pythonorpython3directly:python3 example/index.py
-
Set up pre-commit hooks:
poetry run pre-commit install
Run static type checking:
poetry run mypy .Format all Python files in the project:
# Format with Black and sort imports
poetry run black .
poetry run isort .Run the linter on all Python files:
# Check and fix issues
poetry run ruff check --fix .You can create a simple Makefile to combine formatting, linting, and type checking (cross-platform-friendly with GNU Make installed):
format:
poetry run black .
poetry run isort .
lint:
poetry run ruff check --fix .
typecheck:
poetry run mypy .
check: format lint typecheckThen run:
make checkThis project includes pre-commit hooks to ensure code quality. They will run automatically before each commit and block commits if there are any linting errors or type checking issues.
The pre-commit configuration is in the .pre-commit-config.yaml file.
To manually run all pre-commit hooks:
poetry run pre-commit run --all-filespyproject.toml- Project configuration, dependencies, and tool settings.mypy.ini- Additional MyPy configuration for type checking.pre-commit-config.yaml- Git pre-commit hook configuration.vscode/settings.json- VS Code settings for Python developmentmytypes.py- Example type definitionsindex.py- Example Python file with type hints
The included .vscode/settings.json file will automatically configure VS Code to use the correct formatting and linting settings. Just install the Python extension and select the Poetry virtual environment as your Python interpreter.
PyCharm has excellent support for Poetry. To set up:
- Go to Settings → Project → Python Interpreter
- Click on the gear icon and select "Add..."
- Choose "Poetry Environment" and select your project directory
- PyCharm will automatically detect and use Poetry for the project
For Emacs integration with Poetry, you can use the poetry.el package:
(use-package poetry
:ensure t
:config
(poetry-tracking-mode))# Add a runtime dependency
poetry add <package-name>
# Add a development dependency
poetry add --group dev <package-name>
# Update all dependencies
poetry update
# Update a specific package
poetry update <package-name>
# Export requirements
poetry export -f requirements.txt --output requirements.txt
poetry export --with dev -f requirements.txt --output dev-requirements.txtIf you have a requirements.txt file and you want to install those dependencies, run:
poetry add $(cat requirements.txt)This will install those dependencies and add them to your pyproject.toml file.
You can add pytest or unittest for test coverage.
# Example with pytest
poetry add --group dev pytest
# Run tests
poetry run pytestThis repository includes an example_type_errors.py file that intentionally contains type errors to demonstrate how the configured linters and type checkers work. This file is excluded from pre-commit hooks.
To see type checking in action, run:
poetry run mypy example_type_errors.py
# or
poetry run pyright example_type_errors.pyYou can use GitHub Actions (or any CI provider) to automate linting, type-checking and testing.
Example: .github/workflows/ci.yml
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.