Best suggestion: try them all. It won't take long.
My favourite: Jinja2 (by a mile)
It has decent syntax, can trace errors through it, and is sandboxable.
Answer from Ali Afshar on Stack OverflowBest suggestion: try them all. It won't take long.
My favourite: Jinja2 (by a mile)
It has decent syntax, can trace errors through it, and is sandboxable.
If you're doing code generation, you might find Cog useful - it's specifically for code generation, rather than being a generally applicable templating language.
Generate python code from a generic template
Python Code Generation Tools
code generation - Generate Python templates with parameters - Stack Overflow
An extremely modern and configurable Python project template
Hi,
I need to write a console wizard that will ask several questions, and based on the answers and/or input, it will generate a py file.
For example:
$ python3 wizard.py
Name? IntelBP
Country? USA
Writing file ... Include.py generated.
$ cat include.py
name='IntelBP' country='USA'
... EOF.
So basically, what i need is a wizard engine that asks questions, and based on the answers, it reads a template (python code) and fills in some variables.
So there's essentially 2 aspects to this question:
What package can I use for writing the console interface? Ideally it will support text, lists, single, and multiple values for a specific wizard question.
Are there any packages designed to populate the values from the template into a python file? My greater concern is security related issues such as code injection.
Thanks!!
What do you guys use to generate Python code? I saw that a lot of people just use Jinja2 with some templates, which looks simple and powerful. You still have to generate the environments and write the templates. Now I'm looking around whether someone already did the work of generalizing that step (thinking about fields in classes, inheritance, constructors etc. and how to map that data to the environment) or whether I have to do it myself.
TLDR: Are there simple but powerful Python code generation "frameworks"?
EDIT:
-
I found pymultigen. Seems like it helps with creating code in multiple files.
-
Looks like pyecoregen is a specialized multi-file code generator based on pymultigen.
What My Project Does
Rob's Awesome Python Template is a cookiecutter template meant to bootstrap python projects using modern best practices.
At the very basic level it includes:
-
Modern
pyproject.tomlwithout any legacy files (no setup.py or setup.cfg). -
Development Management using Makefiles.
-
Configuration Management with Pydantic.
-
PyPI Publishing from Git Tags using setuptools-scm.
-
Formatting and Linting with Ruff.
-
Typing with mypy.
-
Lockfiles (requirements.txt, requirements-dev.txt) with uv.
-
Testing with pytest.
-
CI/CD using Github Actions.
-
Precommit Hooks using the precommit framework.
It also has a ton of optional features:
-
Github Actions for CI
-
Cross Platform (arm, arm64, amd64) Docker containers using the Multi-Py project.
-
Optionally use any combination of FastAPI, Click/Typer, Celery, and Sqlalchemy.
I've used this template for a number of projects- QuasiQueue, Paracelsus, and Fedimapper being some nice examples. If you want to see exactly what the project would generate today you can review the examples repository which builds a few projects using different options to give people a feel for what things can look like.
Target Audience
Any developer looking to bootstrap their projects can use this! What makes it really helpful is that you get a modern, high quality project with tools already configured before you even write a single line of code. Even if all you're doing is creating a POC this template can help you do it with style.
Comparison
Although there are other templates out there, this project is unique in a few ways:
-
It is extremely modern, using tools such as
RuffandUVwhile also removing legacy fluff such assetup.py. -
It isn't targeted towards a specific type of software (library, cli, app) but can be used for a variety of project types, or even for projects with multiple entry points.
-
It uses Cookiecutter, rather than direct git forking, to fill in fields and customize the project so you can get started right away after running it.
Try it now!
If you haven't installed Cookiecutter yet it's pretty easy on most platforms. With homebrew, for instance, just run brew install cookiecutter.
From there-
cookiecutter gh:tedivm/robs_awesome_python_template