unity/nuget/README_Python.md
PuerTS is a scripting solution that brings Python (CPython) to your .NET applications. It provides seamless C# interop — using the same unified ScriptEnv API as the JavaScript and Lua backends. Great for AI/ML integration, tooling, and rapid prototyping.
ScriptEnv architecture as JS and Lua backends — easy to switch or mix languages.Install via NuGet. The Complete package includes core + native assets for all desktop platforms:
dotnet add package Puerts.Python.Complete
Or install components separately for finer control:
# Core library (required)
dotnet add package Puerts.Core
# Python backend
dotnet add package Puerts.Python
# Native assets per platform (choose what you need)
dotnet add package Puerts.Python.NativeAssets.Win32
dotnet add package Puerts.Python.NativeAssets.Linux
dotnet add package Puerts.Python.NativeAssets.macOS
.net8.0+, .netstandard2.1
| Windows (x64) | Linux (x64) | macOS (arm64) | |
|---|---|---|---|
| Python | ✔️ | ✔️ | ✔️ |
Note: The macOS native assets currently support
osx-arm64only.Note: AOT compilation is not currently supported.
using Puerts;
var env = new ScriptEnv(new BackendPython());
// Execute Python code
env.Eval("print('Hello from Python!')");
// Eval with return value
int result = env.Eval<int>("1 + 2");
Console.WriteLine($"Python result: {result}"); // output: 3
env.Dispose();
Python is indentation-sensitive. For multi-line code blocks, wrap with exec('''...'''):
var env = new ScriptEnv(new BackendPython());
env.Eval(@"
exec('''
def greet(name):
print('Hello, ' + name + '!')
greet('PuerTS')
''')
");
// output: Hello, PuerTS!
env.Dispose();
using Puerts;
using System;
var env = new ScriptEnv(new BackendPython());
// Get a Python lambda as a C# delegate
var add = env.Eval<Func<int, int, int>>("lambda a, b: a + b");
Console.WriteLine(add(10, 20)); // output: 30
// Define a function via exec, then retrieve it
env.Eval(@"
exec('''
def greet(name):
print('Hello,' + name + '!')
''')
");
var greet = env.Eval<Action<string>>("greet");
greet("PuerTS"); // output: Hello,PuerTS!
env.Dispose();
| Feature | JavaScript | Lua | Python |
|---|---|---|---|
| Environment creation | new ScriptEnv(new BackendV8()) | new ScriptEnv(new BackendLua()) | new ScriptEnv(new BackendPython()) |
| Console output | console.log(...) | print(...) | print(...) |
| Multi-line code | Write directly | Write directly | Wrap with exec('''...''') |
| Accessing C# types | CS.Namespace.Class | require('csharp') | import Namespace.Class |
| Instance method calls | obj.Method() | obj:Method() | obj.Method() |
| Null representation | null / undefined | nil | None |
For full documentation, tutorials and API reference, visit: https://puerts.github.io/en