python-project-generator 2.3.0

Generates a Python project structure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# Python Project Generator

[![Tests Status](https://github.com/sanders41/python-project-generator/actions/workflows/testing.yml/badge.svg?branch=main&event=push)](https://github.com/sanders41/python-project-generator/actions?query=workflow%3ATesting+branch%3Amain+event%3Apush)
![crates.io](https://img.shields.io/crates/v/python-project-generator.svg?color=brightgreen)

Generates a Python project structure with github actions for continuous integration and continuous
deployment. Both pure Python projects and Python projects with Rust modules using PyO3 can be
created. Addtionally FastAPI projects can be generated.

## Pure Python project included packages

For package managment choose between:

- [poetry]https://python-poetry.org/
- [setuptools]https://github.com/pypa/setuptools
- [uv]https://docs.astral.sh/uv/
- [pixi]https://prefix.dev/

Dev packages:

- [mypy]https://www.mypy-lang.org/ for static type checking
- [pytest]https://docs.pytest.org/en/latest/ for testing
- [pytest-cov]https://github.com/pytest-dev/pytest-cov for test coverage reports
- [ruff]https://beta.ruff.rs/docs/ for linting and code formatting

## Python project with Rust modules included packages

- [maturin]https://github.com/PyO3/maturin for package management
- [mypy]https://www.mypy-lang.org/ for static type checking
- [pytest]https://docs.pytest.org/en/latest/ for testing
- [pytest-cov]https://github.com/pytest-dev/pytest-cov for test coverage reports
- [ruff]https://beta.ruff.rs/docs/ for linting and code formatting
- [PyO3]https://github.com/PyO3/pyo3 for managing the Rust/Python FFI
- [justfile]https://github.com/casey/just for running commands (to use this you will need to
  install just)

## FastAPI projects include

- [asyncpg]https://github.com/MagicStack/asyncpg for interacting with PostgreSQL
- [camel-converter]https://github.com/sanders41/camel-converter for converting to and from
  camel/snake case in Pydantic models when serializing/deserializing JSON
- [fastapi]https://github.com/fastapi/fastapi
- [granian]https://github.com/emmett-framework/granian for handling the web requests
- [httptools]https://github.com/MagicStack/httptools for faster http parsing
- [loguru]https://github.com/Delgan/loguru for logging
- [orjson]https://github.com/ijl/orjson for faster JSON serization/deserilization
- [pwdlib]https://github.com/frankie567/pwdlib for password hashing
- [pydantic]https://github.com/pydantic/pydantic for model validation
- [pydantic-settings]https://github.com/pydantic/pydantic-settings for managing settings
- [uvloop]https://github.com/MagicStack/uvloop for enhanced performance (not available on Windows)
- [postgresql]https://www.postgresql.org/ for the database layer
- [valkey]https://github.com/valkey-io/valkey for the caching layer
- [traefik]https://github.com/traefik/traefik for reverse proxy
- [sqlx]https://github.com/launchbadge/sqlx for migrations

## Docs

If you chose to include docs then additional dev packages will be included for docs.

- [mkdocs]index_md for creating the docs
- [mkdocs-material]index_md for theming the docs
- [mkdocstrings]index_md for automatically creating API docs

Additionally the `pypi_publish.yml` workflow will also be setup to deploy the doc on release.

## Installation

Install with `cargo`:

```sh
cargo install python-project-generator
```

If you want to be able to generate FastAPI projects install with the fastapi feature

```sh
cargo install python-project-generator -F fastapi
```

Install on Arch with the AUR:

```sh
paru -S python-project-generator-bin
```

Install on Debian/Ubuntu:

Note: Change the version to match the version you want to install.

```sh
curl -LO https://github.com/sanders41/python-project-generator/releases/download/v1.0.16/python-project-generator_1.0.16_amd64.deb
sudo dpkg -i python-project-generator_1.0.16_amd64.deb
```

Python Project Generator can also be installed with binaries provided with each release
[here](https://github.com/sanders41/python-project-generator/releases), or with cargo.

## How to use

### Create a new project

From your terminal run:

```sh
python-project create
```

You will be asked a series of questions that will be used to generate your project. The project
generator will check pypi for the lastest version of the included packages and use those while
generating the project. This feature can be disabled by using with either `-s` or
`--skip-download-latest-packages` when running the generator. If either there is an issue with
retrieving the latest versions or if you have decided to skip looking up the latest version, the
packages will be be created with default versions.

```sh
python-project create -s
```

#### Options

- License

  Choose from MIT, Apache 2, or no license.

- Python Version

  This will be the default Python version used. For example when releasing the project this is the
  version of Python that will be used.

- Minimum Python Version

  This is the minimum supported Python version for the project. This is also the version that is
  used for ruff's upgrade target version.

- Python Versions for Github Actions Testing

  Versions listed here will be the versions used for testing in CI.

- Project Manager

  Specifies how project dependencies and builds should be handled

- Application or Library

  Choosing application will create `main.py` and `__main__.py` files. Choosing library will omit
  these files. FastAPI projects are automatically created as applications with a special FastAPI
  main.py

- Async Project

  Selecting yes for this option will add [pytest-asyncio]https://github.com/pytest-dev/pytest-asyncio
  to the dev dependencies. Additionally if the project is an application the `main` function will
  be made async. This question is skipped with FastAPI projects and automatically set to an async
  project

- Max Line Length

  This controls how long the ruff formatter will use for line wrapping.

- Use Dependabot

  Dependabot can be used to keep dependencies up to date. If enabled dependabot will automatically
  create PRs to update dependencies when they are available.

- Dependabot Schedule

  When dependabot is enabed the schedule controls how often dependabot will check for updates and
  create PRs.

- Use Continuous Deployment

  This will create a GitHub Action to deploy the project to PyPI when a new release is created.
  Note that for this to work you will need to setup a
  [trusted publisher]https://docs.pypi.org/trusted-publishers/adding-a-publisher/ in PyPI with
  a workflow name of pypi_publish.yml.

  If the project is a FastAPI project this will create workflows to deploy to test and production
  servers using GitHub runners.

- Release Drafter

  Choosing yes will create a [release drafter]https://github.com/release-drafter/release-drafter
  action automatically adds the tile of the PR, who created it, and it's PR number to a draft
  GitHub release. By default the release will get a patch version update. Adding a `bug` label will
  get a patch version update and add it to the `Bug` section of the release notes. Adding an
  `enhancement` label to a PR will create a release with a minor version bump, and a
  `breaking-change` label will create a major version bump. The draft release will get the release
  version tag for the highest label applied to the merged PRs in the release. PRs can be excluded
  from the release notes by applying a `skip-changelog` label to the PR.

- Use Multi OS CI

  Choosing yes will setup CI to run tests on Linux, Mac, and Windows. If no is chosen tests will
  only run on Linux in CI.

  This is skipped for FastAPI projects and defaults to Linux only. FastAPI projects use Docker
  with is only available in Linux in GitHub Actions.

- Include Docs

  Choosing yes will add additional packages and base setup for creating documents with mkdocs.

- Docs Site Name

  This quesion will only show if you chose `yes` for `Include Docs`. This value sets the site name
  field for mkdocs.

- Docs Site Description

  This quesion will only show if you chose `yes` for `Include Docs`. This value provides a
  a description of the repo to use in the docs.

- Docs Site URL

  This quesion will only show if you chose `yes` for `Include Docs`. This is the URL where the docs
  will be hosted.

- Docs Locale

  This quesion will only show if you chose `yes` for `Include Docs`, and controls the language of
  the docs.

- Repo Name

  This quesion will only show if you chose `yes` for `Include Docs`. This is the name of the repo
  the docs are referencing. For example in this repository the repo name would be
  `sanders41/python-project-generator`.

- Repo URL

  This quesion will only show if you chose `yes` for `Include Docs`. This is URL for the repo the
  docs are referencing. For example in this repository the repo url would be
  `https://github.com/sanders41/python-project-generator`

After running the generator a new directory will be created with the name you used for the
`Project Slug`. Change to this directory then install the python packages and pre-commit hooks.

### Pure Python Projects

#### Install the Python dependencies when using Poetry.

```sh
poetry install
```

#### Install the Python dependencies when using setuptools.

First create a virtual environment and activate it.

```sh
python -m venv .venv
. .venv/bin/activate
```

```sh
python -m pip install -r requirements-dev.txt
```

#### Install the Python dependencies when using uv.

First create a virtual environment and activate it.

```sh
uv venv
. .venv/bin/activate
```

Next create a lock file

```sh
uv lock
```

Then install the dependencies

```sh
uv sync --frozen
```

Install the pre-commit hooks.

```sh
pre-commit install
```

### PyO3 projects

First create a virtual environment and activate it.

```sh
python -m venv .venv
. .venv/bin/activate
```

Install the dependencies and build the rust module.

```sh
just install
```

Install the pre-commit hooks.

```sh
pre-commit install
```

### FastAPI projects

Create a .env file with the needed variables. the .env-example file can be used as a starter
template. Then start the containers.

```sh
docker compose up
```

Now your project is ready to use.

### Save custom default values

You can specify default values for many of the project options. For example to save a default
creator:

```sh
python-project config creator "Wade Watts"
```

To see a full list of values that be set as defaults run:

```sh
python-project config --help
```

To view the current saved defaults:

```sh
python-project config show
```

To remove custom defaults:

```sh
python-project config reset
```

## Information

### just

[just](https://github.com/casey/just) allows you to add project specific commands to the project
that can be run from the command line. It is very similar to [make](https://github.com/mirror/make)
but has the advantage of being cross platform compatable and purposee build for running commands.

As an example, if you have the following in the `justfile` (this is included with the default file
generated by this project):

```just
@_default:
  just --list

@lint:
  echo mypy
  just --justfile {{justfile()}} mypy
  echo ruff-check
  just --justfile {{justfile()}} ruff-check
  echo ruff-format
  just --justfile {{justfile()}} ruff-format

@mypy:
  uv run mypy my_project tests

@ruff-check:
  uv run ruff check my_project tests

@ruff-format:
  uv run ruff format my_project tests

@test *args="":
  -uv run pytest {{args}}

@lock:
  uv lock

@lock-upgrade:
  uv lock --upgrade

@install:
  uv sync --frozen --all-extras
```

Then you can run `mypy`, `ruff` check, and `ruff` format with:

```sh
just mypy
just ruff-check
just ruff-format
```

You can also run all 3 with 1 `just` command:

```sh
just lint
```

### pre-commit

[pre-commit](https://pre-commit.com/) runs linting and formatting on your code (as defined in the
provided .pre-commit-config.yaml file) every time you make a commit to your code. If any of the
lints fail pre-commit will cancel the commit. When possible, pre-commit will also automatically
fix any errors that fail. For example pre-commit can automatically apply changes that fail ruff
fromatting. pre-commit caches information and only runs on files that have changed so it is fast
and doesn't slow down your work flow will preventing you from forgetting to run checks.

### FastAPI migrations

[sqlx](https://github.com/launchbadge/sqlx) is used for migrations. A dedicated docker container
runs the migrations each time docker is started. For creating new migrations install `sqlx-cli`.
`sqlx-cli` also needs to be instealled in order to run the generated test suite.

```sh
cargo install sqlx-cli --no-default-features --features native-tls,postgres
```

Then to add a new migration run:

```sh
sqlx migrate add -r my_migration
```

This will create new migration up and down files in the migrations directory.

## Contributing

If you are interested in contributing please see our [contributing guide](CONTRIBUTING.md)