Skip to main content

Python to Rust, C++, Go, Zig, Mojo & More - Universal Python Transpiler

Project description

py2many: Python to Rust, C++, Go, Zig, Mojo & More - Universal Python Transpiler

Build License

Convert Python code to Rust, C++, Go, Zig, Julia, Nim, Dart, and other languages automatically

py2many is a powerful Python transpiler that converts Python source code into multiple statically-typed programming languages. Transform your Python code to Rust for performance, C++ for systems programming, Go for concurrency, or Kotlin for mobile development.

Why Convert Python to Other Languages

Performance: Python is popular and easy to program in, but has poor runtime performance. Transpiling Python to Rust, C++, or Go can dramatically improve execution speed while maintaining the development experience of Python.

Security: Writing security-sensitive code in low-level languages like C is error-prone and could lead to privilege escalation. With py2many, you can write secure code in Python, verify it with unit tests, then transpile to a safer systems language like Rust.

Cross-platform Development: Accelerate Python code by transpiling it into native extensions or standalone applications.

Mobile & Systems Programming: While Swift and Kotlin dominate mobile app development, there's no universal solution for sharing lower-level library code between platforms. py2many provides an alternative to Kotlin Mobile Multiplatform (KMM) by letting you write once in Python and deploy to multiple targets.

Learning Tool: It's an excellent educational tool for learning new programming languages by comparing Python implementations with their transpiled equivalents.

Supported Languages & Status

Primary Focus: Python to Rust conversion with the most mature feature set and active development.

Production Ready: Python to C++ transpilation (C++14 historically supported, C++17+ required for advanced features).

Beta Support: Python to Julia, Python to Kotlin, Python to Nim, Python to Go, Python to Dart, Python to V, and Python to D transpilation.

Type Inference: py2many can also emit enhanced Python 3 code with inferred type annotations and syntax improvements for better code analysis.

Python to Rust Example

See how py2many converts Python code to idiomatic Rust:

Original Python code:

def fib(i: int) -> int:
    if i == 0 or i == 1:
        return 1
    return fib(i - 1) + fib(i - 2)

# Demonstrate overflow handling
def add(i: i32, j: i32):
    return i + j

Transpiled Rust code:

fn fib(i: i32) -> i32 {
    if i == 0 || i == 1 {
        return 1;
    }
    return (fib((i - 1)) + fib((i - 2)));
}

// return type is i64
pub fn add(i: i32, j: i32) -> i64 {
    return ((i as i64) + (j as i64)) as i64;
}

More Examples: View transpiled code for all supported languages at: https://github.com/adsharma/py2many/tree/main/tests/expected (fib*)

Quick Start: Convert Python to Rust, C++, Go & More

Requirements:

  • Python 3.8+

Installation:

pip3 install --user  # installs to $HOME/.local

OR

sudo pip3 install  # installs systemwide

Usage Examples:

Convert Python to different languages:

# Python to Rust
py2many --rust tests/cases/fib.py

# Python to C++
py2many --cpp tests/cases/fib.py

# Python to Go
py2many --go tests/cases/fib.py

# Python to Kotlin
py2many --kotlin tests/cases/fib.py

# Python to Julia
py2many --julia tests/cases/fib.py

# Python to Nim
py2many --nim tests/cases/fib.py

# Python to Dart
py2many --dart tests/cases/fib.py

# Python to D
py2many --dlang tests/cases/fib.py

Compiling Transpiled Code:

# Compile C++
clang tests/expected/fib.cpp

# Run Rust
./scripts/rust-runner.sh run tests/expected/fib.rs

# Run D
dmd -run tests/cases/fib.d

Language-Specific Tools:

py2many integrates with language-specific formatters and tools:

  • rustfmt for Rust code formatting
  • Language-specific standard libraries and external dependencies

For detailed setup instructions for each target language, see .github/workflows/main.yml.

Key Features

  • Multi-Language Support: Convert Python to 8+ programming languages
  • Type Inference: Automatically infer and convert Python types to target language types
  • Performance Optimization: Generate optimized code for systems programming languages
  • Cross-Platform: Works on Linux, macOS, and Windows
  • Open Source: MIT licensed with active community development
  • Educational: Compare Python implementations with transpiled code to learn new languages

Use Cases

  • Performance-Critical Applications: Convert Python algorithms to Rust or C++ for speed
  • Systems Programming: Transform Python prototypes to systems languages
  • Mobile Development: Convert Python logic to Kotlin for Android development
  • WebAssembly: Transpile Python to Rust for WASM deployment
  • Embedded Systems: Convert Python code to C++ or Rust for resource-constrained environments
  • Cross-Platform Libraries: Write once in Python, deploy to multiple language ecosystems

Project History

Based on Julian Konchunas' pyrs.

Based on Lukas Martinelli Py14 and Py14/python-3 branch by Valentin Lorentz.

Contributing

See CONTRIBUTING.md for how to test your changes and contribute to this project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py2many-0.8-py3-none-any.whl (165.7 kB view details)

Uploaded Python 3

File details

Details for the file py2many-0.8-py3-none-any.whl.

File metadata

  • Download URL: py2many-0.8-py3-none-any.whl
  • Upload date:
  • Size: 165.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for py2many-0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 11220932d779a3feb441e63c8e5312bfd34f48a3d23da661511690647e2153b0
MD5 72745f7d35411fb92882b6229e45e114
BLAKE2b-256 effbae57aa812b7755e9a83afd27c1408bd243f45e1d631a14f6947add262782

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page