How to initialize a new python project with user's desired settings
To initialize a Python project, you can follow these steps:
Create a new directory for your project and navigate into it:
mkdir my-python-project
cd my-python-project
Use uv to initialize the project base files : pyproject.toml, README.md, .gitignore
uv init
Add the hatchling build system to the pyproject.toml file:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["src/my_python_project"]
Add a directory for the main module, and the empty base module file for the project
mkdir -p src/my_python_project
touch src/my_python_project/__init__.py
All commands should be run from the root of the project and wrapped in a uv run command, to ensure that they are run in the virtual environment. For example, to run the project, you can use the following command:
uv run python src/my_python_project
pytest is the testing framework that we will use for this project. To add it as a development dependency, run the following command:
uv add --group=dev pytest pytest-asyncio pytest-timeout pytest-cov
mkdir tests touch tests/init.py
* Add this section into the pyproject.toml file to configure pytest:
```toml
[tool.pytest.ini_options]
pythonpath = ["src"]
testpaths = ["tests"]
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
def pytest_report_teststatus(report, config):
if report.when == "call":
if report.failed:
return report.outcome, "F", "❌ FAIL"
elif report.passed:
return report.outcome, ".", "✅ PASS"
uv run python -m compileall src
uv run pytest --tb=short --maxfail=1 --failed-first --no-header --color=no
Linting, formatting and import sorting is done using ruff. To add it as a development dependency, run the following command:
uv add --group=dev ruff
[tool.ruff]
line-length = 88
target-version = "py313"
exclude = [
".git", ".venv", "__pycache__", "build", "dist"
]
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F"]
ignore = ["E501"] # Ignore line length errors
fixable = ["ALL"]
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
uv add --group=dev mypy
[tool.mypy]
python_version = 3.13
check_untyped_defs = true
disallow_untyped_defs = true
ignore_missing_imports = true
uv run ruff check --fix
uv add --group=dev bandit
[tool.bandit]
skips = ["B101"] # Skip assert statements, as they can be used for testing
exclude = ["tests"] # Exclude the tests directory from security analysis
uv run bandit -r src/my_python_project
uv add --group=dev pysentry-rs
uv run pysentry-rs
uv add command. For example, to add the httpx library as a dependency, run the following command:uv add httpx