Py2Cpp is a production-grade Python to C++20 transpiler. It converts Python scripts into optimized, memory-safe C++ code with zero dependencies on the core AST translation engine. Perfect for turning Python prototypes into high-performance C++ binaries.
- β Zero-dependency core AST translation
- β Intelligent type inference engine
- β Memory-safe C++20 generation
- β Full scope & symbol tracking
- β Optimized g++ compilation pipeline
- β Comprehensive error handling
- π₯ CLI interface for rapid transpilation and execution
pip install typer-
Download and install MSYS2
-
Update package database:
pacman -Syu- Install g++ and make:
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-make- Add
C:\msys64\mingw64\binto your PATH.
pip install -r requirements.txtrequirements.txt should contain:
typerpy2cpp --helpYou should see the CLI usage instructions.
py2cpp <input_file.py> [OPTIONS]| Option | Shortcut | Description |
|---|---|---|
--output |
-o |
Specify output binary path |
--std |
Set C++ standard (default: c++20) | |
--optimize |
-O |
Optimization level (O0, O1, O2, O3, Os) |
--run |
-r |
Run binary after compilation |
--keep-cpp |
-k |
Keep generated .cpp file |
--verbose |
-v |
Enable verbose logging |
py2cpp main.py --run --optimize O3- AST Parsing & Analysis: Parses Python AST, extracts type information, and analyzes scopes.
- Type Inference: Infers C++ types for variables, functions, containers, and constants.
- C++ Code Generation: Generates fully scoped, memory-safe C++20 code including runtime preamble for Python builtins.
- Compilation: Invokes g++ with selected optimization level and generates executable binary.
- Execution: Optionally runs the generated binary and reports execution stats.
- Variables, constants, and basic types
- Lists (
list) βstd::vector - Dicts (
dict) βstd::map - Loops:
for,while - Conditional statements:
if,elif,else - Functions & methods, including
__init__ - String formatting, arithmetic, comparisons, boolean operations
- Return type inference
- Some dynamic Python constructs may fall back to
std::any - Certain built-in functions are mapped explicitly (
input,print,len,range) - Does not yet support async or coroutine features
-
βΉοΈInfo messages -
βSuccess -
β οΈWarnings -
βErrors -
Final report includes:
- Duration
- Nodes processed
- Warnings & errors
- Status (SUCCESS/FAILED)
std::vector<int> nums = {1, 2, 3};
for (int i = 0; i < 3; i++) {
print(nums[i]);
}MIT License Β© 2026
- Soumalya - Creator & Maintainer
- Email: geniussantu1983@gmail.com
- GitHub: Soumalya