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
🌐
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
Discussions

code generation - Are there any libraries for generating Python source? - Stack Overflow
I'd like to write a code generation ... generated classes. I can create the class and use it in code, but it would be nice to have a sourcefile both for documentation and to allow something to import. Does such a thing exist? I've seen sourcecodegen, but I'd rather avoid messing with the ast trees as they're not portable. ... I'm not aware of any off-the-shelf library, but have a look at the Python templating ... More on stackoverflow.com
🌐 stackoverflow.com
code generation - Python generating Python - Stack Overflow
This is pretty much the best way to generate Python source code. However, you can also generate Python executable code at runtime using the ast library. You can build code using the abstract syntax tree, then pass it to compile() to compile it into executable code. More on stackoverflow.com
🌐 stackoverflow.com
Will devs be replaced by AI? I found this ai python code generator and it's terrifyingly good
Sooner than later there will be an indirect code leak where someone will train one of such public models on their non-public code... And if the model won't be trained it will not give matching results... or if it will be trained and run in-house it will require the hardware and the specialist to do so which isn't free or cheap. Simpler stuff like setup an application or module will work, but when you tell it to generate a scaffold for specific API provider or module it was never trained on it will generate rubbish assuming it is the same as APIs/modules it was trained on. Not to mention that even if the code gets generated it has to be tested, it has to be integrated properly and so on. But wannabies, 50-subscriber YouTubers that still produce "Python string for beginners 2023" have yet another reason to be afraid they will never get an actual job. More on reddit.com
🌐 r/Python
11
0
May 22, 2023
Code generation

Preface your code generation with exact specifications:

### Python 3.7+, xxxx.py version 2.3

The fault in code generation is simply the feeble-minded-ness of ChatGPT as of late. It will screw up code again you had just made it fix. You must pretty much limit yourself to just making small functions it can understand.

If you want power programming, you can use the non-chat API, as I show doing a different task:

https://www.reddit.com/r/ChatGPT/comments/1317t3x/use_openai_gpt_models_and_api_to_be_your/

More on reddit.com
🌐 r/ChatGPT
2
3
May 14, 2023
🌐
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.
🌐
GitHub
github.com › modm-io › lbuild
GitHub - modm-io/lbuild: lbuild: a generic, modular code generator in Python 3 · GitHub
The Library Builder (pronounced lbuild) is a BSD licensed Python 3 tool for describing repositories containing modules which can copy or generate a set of files based on the user provided data and options. lbuild allows splitting up complex ...
Starred by 43 users
Forked by 11 users
Languages   Python
🌐
Qodo
qodo.ai › blog › news & updates › top 7 python code generator tools in 2025
Top 7 Python Code Generator Tools in 2025 - Qodo
May 16, 2025 - IntelliCode provides thousands ... VS Code. By clicking on a suggestion, developers can browse through the examples sourced from public repositories, helping them understand how to implement specific functions or libraries effectively. While IntelliCode is particularly useful for the Python language, ...
🌐
PyPI
pypi.org › project › codegenlib
codegenlib · PyPI
Code Generation library written by python. can use bash script, can be extend python code, can use mustache files or can use any string content for any code generation structure.
      » pip install codegenlib
    
Published   Jun 16, 2022
Version   0.8.0
🌐
SymPy
sympy.org › scipy-2017-codegen-tutorial
Automatic Code Generation with SymPy
SymPy supports generating code ... can easily be extended to other languages. SymPy’s code generation is used by libraries such as PyDy, pyodesys, sympybotics, pycalphad, and many other programs....
Find elsewhere
🌐
CodingFleet
codingfleet.com › code-generator › python
Python Code Generator - CodingFleet
Meet Python Code Generator - an innovative AI-powered tool that transforms your instructions into efficient Python code. Just say what you need, and it'll generate the code. It's like having your very own Python wizard!
🌐
Kaggle
kaggle.com › code › muhammadumairab › python-code-generation
Python Code Generation
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
Zeroc
archive.zeroc.com › ice › 3.6 › language-mappings › python-mapping › client-side-slice-to-python-mapping › code-generation-in-python
Code Generation in Python - Ice
You should be familiar with static code generation if you have used other Slice language mappings, such as C++ or Java. Using static code generation, the Slice compiler slice2py generates Python code from your Slice definitions.
🌐
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
🌐
Workik
workik.com › python-code-generator
FREE AI Python Code Generator: Generate, Debug, & Test Code Online
Just paste a code snippet using libraries like NumPy, Requests, or SQLAlchemy, and Workik AI will break it down, explain the purpose, and even rewrite it with inline comments. It’s ideal for onboarding into unfamiliar Python stacks.
🌐
GitHub
github.com › cogu › cfile
GitHub - cogu/cfile: A python C code generator
A python C code generator. Contribute to cogu/cfile development by creating an account on GitHub.
Starred by 91 users
Forked by 25 users
Languages   Python 99.7% | Batchfile 0.3% | Python 99.7% | Batchfile 0.3%
🌐
Mark's Software Blog
markvtechblog.wordpress.com › 2024 › 04 › 28 › code-generation-in-python-with-jinja2
C++ Code Generation using Python and Jinja2 | Mark's Software Blog
January 31, 2026 - A template for CG is a text file with special variables that represent things like class names, constructors, class variables, etc. To keep this simple I am only showing how to generate C++ classes. The tool I chose for rendering the generated code is Jinja. Jinja is a Python library.
🌐
PyPI
pypi.org › project › code-generation
code-generation · PyPI
Simple and straightforward code generator for creating C++ code. It also could be used for generating code in any programming language. Written in Python, works both with Python 2 and 3
      » pip install code-generation
    
Published   Jan 31, 2023
Version   2.3.0
🌐
Medium
medium.com › @yonatanzunger › advanced-python-achieving-high-performance-with-code-generation-796b177ec79
Advanced Python: Achieving High Performance with Code Generation | by Yonatan Zunger | Medium
August 11, 2022 - In the first part, we synthesize the bytecode and line number tables, by the most direct fashion possible — writing to a BytesIO object, and getting the requisite binary opcode values out of the dis library.