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 Overflow
๐ŸŒ
GitHub
github.com โ€บ pyscaffold โ€บ pyscaffold
GitHub - pyscaffold/pyscaffold: ๐Ÿ›  Python project template generator with batteries included
PyScaffold is a project generator for bootstrapping high quality Python packages, ready to be shared on PyPI and installable via pip. It is easy to use and encourages the adoption of the best tools and practices of the Python ecosystem, helping ...
Starred by 2.3K users
Forked by 185 users
Languages ย  Python
Discussions

Generate python code from a generic template
This sounds like an X-Y question, I think you should describe your larger goal rather than asking how to implement this specific solution. Generating code is usually a bad idea and usually not necessary. More on reddit.com
๐ŸŒ r/learnpython
7
0
February 7, 2023
Python Code Generation Tools
I'd probably use metaclasses if I had the kind of problem where code generation could actually help. The last time I saw a project using code generation where I thought that it ought have to used metaclasses was a tool to generate python code from swagger descriptions. Django's ORM is a good example where metaclasses were used to good effect (there are very few of those). In general I'm extremely suspicious of anybody who claims to have the kind of problem that requires either metaclasses or code generation would help though. It is suitable only for problems that are very, very, very abstract (e.g. like an ORM or REST API interaction framework). In 90% of cases I've seen the person attempting to use it vastly overgeneralized the problem that they actually had in an attempt to prepare for future problems never arose. In the process of doing that they then created a code clusterfuck that was hellishly hard to understand and debug (and in some cases I was left to clean up the mess). It wasn't simple and it can't, intrinsically, ever be simple (there is some language theory behind this). I could be convinced otherwise by describing your problem, but right now I'm 80% convinced that what you have described is an XY problem and that you're attempting to do this because you overgeneralized your problem. If you describe the actual problem you're solving it might help. More on reddit.com
๐ŸŒ r/Python
18
8
March 9, 2019
code generation - Generate Python templates with parameters - Stack Overflow
I have used JET with Eclipse before to generate Java files from models. I am new to Python and I am searching for a way to generate python modules automatically based on parameters (data) read from... More on stackoverflow.com
๐ŸŒ stackoverflow.com
An extremely modern and configurable Python project template
Uv is neat but in my own experiments it isn't quite ready for production. And it suffers from the same problem as pip-tools in that the lock files are specific to the platform they were generated on. So if you lock using MacOS with an M1 CPU you aren't guaranteed to be able to build for x86 Linux in a container. The chances are extremely slim about an incompatibility in that direction instead of the inverse, but the point is that the tooling does nothing to prevent this. By putting your dev deps in the optional dependencies section it actually messes with the metadata of the project for some tools that use that. Because now it looks like your project has a dependency of my and ruff, which it doesn't. The human based task of development is what has those dependencies. A great example of this is if you go to libraries io and search for packages that depend on mypy your project will show up. Some languages has first class support for development dependencies, and Python is not one of them. Best you can do is move it to a requirements-dev.in and still let UV lock it, but not have it show up in the metadata of the project. Why celery in 2024? It is such a bloated platform from years of scope creep. The fact that it does so much at the same time means it either does some of it less than optimal or has a high learning curve, and my experience is that it suffers from both. While your template is correct you may want to clarify you are using pydantic-settings as they are now two different projects. I honestly can't use python without Pydantic and Pydantic-settings anymore, it is just too great of a package. Other than that I think the template is great and has shown me quite a few things I need to look into myself. Thank you for sharing this. More on reddit.com
๐ŸŒ r/Python
22
146
March 2, 2024
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ Templating
Templating - Python Wiki
moody-templates - A fast, extensible templating engine for Python 3 with Django-like syntax. Myghty inspired by Perl's Mason, replaced by Mako and MyghtyUtils. Qpy provides a convenient mechanism for generating safely-quoted html text from python code.
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ pymultigen
Client Challenge
JavaScript is disabled in your browser ยท Please enable JavaScript to proceed ยท A required part of this site couldnโ€™t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ generate python code from a generic template
r/learnpython on Reddit: Generate python code from a generic template
February 7, 2023 -

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:

  1. 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.

  2. 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!!

๐ŸŒ
Cheetahtemplate
cheetahtemplate.org
Cheetah3, the Python-Powered Template Engine โ€” Cheetah3 - The Python-Powered Template Engine
Cheetah3 is a free and open source template engine and code-generation tool written in Python.
๐ŸŒ
YouTube
youtube.com โ€บ codegpt
python code template generator - YouTube
Instantly Download or Run the code at https://codegive.com in software development, maintaining a consistent code structure and style is crucial for readabi...
Published ย  February 19, 2024
Views ย  44
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ python code generation tools
r/Python on Reddit: Python Code Generation Tools
March 9, 2019 -

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.

Top answer
1 of 3
2
I'd probably use metaclasses if I had the kind of problem where code generation could actually help. The last time I saw a project using code generation where I thought that it ought have to used metaclasses was a tool to generate python code from swagger descriptions. Django's ORM is a good example where metaclasses were used to good effect (there are very few of those). In general I'm extremely suspicious of anybody who claims to have the kind of problem that requires either metaclasses or code generation would help though. It is suitable only for problems that are very, very, very abstract (e.g. like an ORM or REST API interaction framework). In 90% of cases I've seen the person attempting to use it vastly overgeneralized the problem that they actually had in an attempt to prepare for future problems never arose. In the process of doing that they then created a code clusterfuck that was hellishly hard to understand and debug (and in some cases I was left to clean up the mess). It wasn't simple and it can't, intrinsically, ever be simple (there is some language theory behind this). I could be convinced otherwise by describing your problem, but right now I'm 80% convinced that what you have described is an XY problem and that you're attempting to do this because you overgeneralized your problem. If you describe the actual problem you're solving it might help.
2 of 3
2
If you're looking for some codegen support stuffs in python, I'd give you some info Python std module ast AST to python source code emitter, unparse. https://github.com/python/cpython/blob/master/Tools/parser/unparse.py AST based scoping analyzer, https://github.com/thautwarm/scoping-resolver
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ metaprogramming โ€บ python-metaprogramming-exercise-9.php
Python Code Generation: Transform Templates
December 21, 2024 - Learn to generate Python code from templates using the 'generate_code' function. Replace placeholders with provided values to create dynamic code. Code example & explanation included.
๐ŸŒ
EA-Bridge
itemis.com โ€บ en โ€บ products โ€บ itemis-create โ€บ documentation โ€บ user-guide โ€บ codegen_python_code_generator
Python code generator
Activating the runtime template feature generates a default_runtime.py file, which supports basic functionalities to run a state machine. Custom code for use case depending projects can be added at the commented areas. Calling the state machine using the template ensures initializing and entering the state machine.
๐ŸŒ
Etlcpp
etlcpp.com โ€บ generators_tutorial.html
generators_tutorial
To this end, the ETL supplies 'generators' to enable the creation of customised variants of these template classes. The code generation is handled by a Python library called COG.
๐ŸŒ
Full Stack Python
fullstackpython.com โ€บ template-engines.html
Template Engines - Full Stack Python
The base.html Jinja template used to generate Full Stack Python allows every page on the site to have consistent HTML but dynamically generate the pieces that need to change between pages when the static site generator executes. The below code from the base.html template shows that the meta ...
๐ŸŒ
Kangz
blog.kangz.net โ€บ posts โ€บ 2016 โ€บ 08 โ€บ 31 โ€บ code-generation-the-easier-way
Code generation the easier way - Corentin's blog
August 31, 2016 - I chose Jinja2 as a template library because it is for the scripting language I know best (but not well), Python, is popular, maintained and has great documentation. After parsing command line arguments and handling special arguments like --print-dependencies for build system integration, the generator ...
๐ŸŒ
GitHub
gist.github.com โ€บ calebrob6 โ€บ eb98725187425c527567
General python program template ยท GitHub
General python program template. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
Blue Book
lyz-code.github.io โ€บ blue-book โ€บ coding โ€บ python โ€บ python_project_template
Python Project template - The Blue Book
I've automated the creation of the python project skeleton following most of these section guidelines with this cookiecutter template. cruft create https://github.com/lyz-code/cookiecutter-python-project
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ an extremely modern and configurable python project template
r/Python on Reddit: An extremely modern and configurable Python project template
March 2, 2024 -

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.toml without 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 Ruff and UV while also removing legacy fluff such as setup.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
Top answer
1 of 5
31
Uv is neat but in my own experiments it isn't quite ready for production. And it suffers from the same problem as pip-tools in that the lock files are specific to the platform they were generated on. So if you lock using MacOS with an M1 CPU you aren't guaranteed to be able to build for x86 Linux in a container. The chances are extremely slim about an incompatibility in that direction instead of the inverse, but the point is that the tooling does nothing to prevent this. By putting your dev deps in the optional dependencies section it actually messes with the metadata of the project for some tools that use that. Because now it looks like your project has a dependency of my and ruff, which it doesn't. The human based task of development is what has those dependencies. A great example of this is if you go to libraries io and search for packages that depend on mypy your project will show up. Some languages has first class support for development dependencies, and Python is not one of them. Best you can do is move it to a requirements-dev.in and still let UV lock it, but not have it show up in the metadata of the project. Why celery in 2024? It is such a bloated platform from years of scope creep. The fact that it does so much at the same time means it either does some of it less than optimal or has a high learning curve, and my experience is that it suffers from both. While your template is correct you may want to clarify you are using pydantic-settings as they are now two different projects. I honestly can't use python without Pydantic and Pydantic-settings anymore, it is just too great of a package. Other than that I think the template is great and has shown me quite a few things I need to look into myself. Thank you for sharing this.
2 of 5
30
I can appreciate the hard work you put into this and why youโ€™d use it for your projects. But this is waaaaay too much and the kitchen sink for me.
๐ŸŒ
Python
python.org โ€บ about โ€บ success โ€บ cog
Cog: A Code Generation Tool Written in Python
For each chunk of generator code it finds, Cog will: ... In a word, great. We now have a powerful tool that lets us maintain a single XML file that describes our data schema. Developers changing the schema have a simple tool to run that generates code from the schema, producing output code in four different languages across 50 files.