Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tonybaloney/csnakes
Embed Python in .NET
https://github.com/tonybaloney/csnakes
csharp dotnet python
Last synced: 3 days ago
JSON representation
Embed Python in .NET
- Host: GitHub
- URL: https://github.com/tonybaloney/csnakes
- Owner: tonybaloney
- License: mit
- Created: 2024-07-18T04:47:22.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-11-29T02:55:44.000Z (27 days ago)
- Last Synced: 2024-12-08T13:49:57.525Z (17 days ago)
- Topics: csharp, dotnet, python
- Language: C#
- Homepage: https://tonybaloney.github.io/CSnakes/
- Size: 1.53 MB
- Stars: 352
- Watchers: 10
- Forks: 24
- Open Issues: 66
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CSnakes - a tool for embedding Python into .NET projects
> [!WARNING]
> This project is in prototype stage and the API is subject to change.[Documentation](https://tonybaloney.github.io/CSnakes/)
[![NuGet Version](https://img.shields.io/nuget/v/CSnakes.Runtime?label=CSnakes.Runtime)](https://www.nuget.org/packages/CSnakes.Runtime)
CSnakes is a .NET Source Generator and Runtime that you can use to embed Python code and libraries into your .NET Solution without the need for REST, HTTP, or Microservices.
![Architecture diagram of CSnakes](docs/res/architecture_simple.png)
## Features
- Supports .NET Standard 8-9
- Supports Python 3.9-3.13
- [Supports Virtual Environments and C-Extensions](https://tonybaloney.github.io/CSnakes/getting-started/#using-virtual-environments)
- Supports Windows, macOS, and Linux
- [Tight integration between NumPy ndarrays and Spans, 2D Spans and TensorSpans (.NET 9)](https://tonybaloney.github.io/CSnakes/buffers/)
- Uses Python's C-API for fast invocation of Python code directly in the .NET process
- Uses Python type hinting to generate function signatures with .NET native types
- Supports CPython 3.13 "free-threading" mode
- Supports nested sequence and mapping types (`tuple`, `dict`, `list`)
- Supports default values## Examples
Given the following Python file called `example.py`
```python
def hello_world(name: str, age: int) -> str:
return f"Hello {name}, you must be {age} years old!"
```CSnakes will generate a static .NET class called `Example` with the function:
```csharp
public class Example {
public static string HelloWorld(string name, long age) {
...
}
}
```When called, `HelloWorld()` will invoke the Python function from `example.py` using Python's C-API and return native .NET types.
## FAQ
See the [FAQ](docs/faq.md) for more information.