Python Project Initialization
Process Overview
- Confirm Project Information - Interact with users to confirm project name and Python version
- Initialize Project - Use uv init to create the project and enable git version control
- Configure Code Style Checking - Use pre-commit to manage and initialize code style checking hooks, and apply corresponding specification configurations
- Configure Testing Framework - Add pytest and pytest-cov as development dependencies
- Configure Version Management Tools - Optimize VCS management by configuring .gitignore, set up version metadata, and configure bumpversion for semantic versioning
- Add Common Development Tools - Add ipython for local debugging
- Install and Verify - Install all dependencies and verify configurations
- Update README - Create and update the README file with project usage instructions
- Complete Initial Commit
Step 1: Confirm Project Information
Confirm the following information with users:
- Project Name (project_name): Defaults to the current directory name
- Project Description (project_description): A short project description to quickly understand the project's function and positioning
- Python Version (python_version): Recommended to use officially supported versions (3.10 and above)
Step 2: Initialize Project
Use uv to initialize the project and enable git version control:
bash
uv init \
# Project name
--name=<project_name> \
# Project description
--description="<project_description>" \
# Initialize src/<project_name> package directory
--package \
# Initialize git
--vcs=git \
# Minimum supported Python version
--python=<python_version>
Step 3: Configure Code Style Checking
Add pre-commit and code formatting tools:
bash
uv add --dev pre-commit black flake8 flake8-import-order ruff
Write the content of
assets/pre-commit-config.yaml to the
file in the project root directory
Apply the template
assets/flake8.j2 to the
file in the project root directory, replacing the variable
with the project name
Install pre-commit hooks:
bash
uv run pre-commit install
Step 4: Configure Testing Framework
Add pytest and pytest-cov as development dependencies:
bash
uv add --dev pytest pytest-cov
Create tests directory and initial test files:
bash
mkdir -p tests
touch tests/__init__.py
Add pytest configuration to
:
toml
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_classes = ["Test*", "*Test"]
python_functions = ["test_*"]
addopts = [
"-v",
"--cov=src/<project_name>",
"--cov-report=term-missing",
"--tb=short",
]
[tool.coverage.run]
source = ["src/<project_name>"]
branch = true
omit = ["tests/*"]
[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"def __repr__",
"raise NotImplementedError",
"if __name__ == .__main__.:",
]
Step 5: Configure Version Management Tools
Add .gitignore
bash
curl https://raw.githubusercontent.com/github/gitignore/refs/heads/main/Python.gitignore -o .gitignore
Add bumpversion dependency:
Configure Version Metadata
-
Create a
src/<project_name>/_version.py
file to store project version metadata
-
Export version information to the project package namespace, adjust
src/<project_name>/__init__.py
python
"""<project_name> package."""
from ._version import __version__ # noqa: F401
-
Add test case to verify version metadata retrieval
python
from <project_name> import __version__
def test_version():
assert __version__
Apply the template
assets/bumpversion.cfg.j2 to the
file in the project root directory, replacing the variable
with the project name
Step 6: Add Common Development Tools
Step 7: Install and Verify
Install all dependencies:
Verify configurations:
bash
# Verify pytest
uv run pytest
# Verify pre-commit
uv run pre-commit run --all-files
# Verify bumpversion
uv run bumpversion --help
# Verify version metadata
uv run python -c 'from <project_name> import __version__; print(__version__)'
Step 8: Update README
Generate the project README file using the template assets/README.md.j2, replacing the following variables:
- - Project name
{{ project_description }}
- Project description
Write the generated content to the
file in the project root directory.
Step 9: Complete Initial Commit
bash
git add .
git commit -m "Initial project setup"