pythonnet: Seamless Python and .NET Integration

Summary
pythonnet offers Python programmers seamless integration with the .NET Common Language Runtime (CLR), enabling powerful application scripting for .NET developers. It allows Python code to interact directly with the CLR and can also be used to embed Python within .NET applications.
Repository Info
Introduction
pythonnet is a powerful package that bridges the gap between Python and the .NET Common Language Runtime (CLR). It provides Python programmers with nearly seamless integration with .NET, allowing them to interact with .NET assemblies and objects directly from Python code. Conversely, .NET developers can leverage pythonnet to embed Python as a scripting engine within their .NET applications, opening up new possibilities for extensibility and dynamic behavior.
Installation
Installing pythonnet is straightforward. You can typically install it using pip or conda-forge for Python environments, and via NuGet for .NET projects.
pip install pythonnet
Or using conda-forge:
conda install -c conda-forge pythonnet
For .NET applications, you can add the pythonnet NuGet package. By default, pythonnet uses Mono on Linux and macOS, and .NET Framework on Windows. For specific runtime configurations, such as .NET Core, refer to the official documentation.
Examples
Calling .NET Code from Python
pythonnet allows CLR namespaces to be treated essentially as Python packages. You can load assemblies using clr.AddReference().
import clr
from System import String
from System.Collections import *
# Load a .NET assembly
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form
# Example for .NET Core runtime
from pythonnet import load
load("coreclr") # Explicitly load .NET Core
import clr
Embedding Python in .NET
To embed Python in a .NET application, you need to initialize the Python engine and manage the Global Interpreter Lock (GIL). Remember to set the Runtime.PythonDLL property or PYTHONNET_PYDLL environment variable before initialization.
using System;
using System.Collections.Generic;
using Python.Runtime;
public class Program
{
public static void Main(string[] args)
{
// Set Python DLL path, e.g., "python38.dll" on Windows
// Runtime.PythonDLL = "python38.dll"; // Uncomment and set appropriate path
PythonEngine.Initialize();
using (Py.GIL())
{
dynamic np = Py.Import("numpy");
Console.WriteLine(np.cos(np.pi * 2));
dynamic sin = np.sin;
Console.WriteLine(sin(5));
double c = (double)(np.cos(5) + sin(5));
Console.WriteLine(c);
dynamic a = np.array(new List<float> { 1, 2, 3 });
Console.WriteLine(a.dtype);
dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
Console.WriteLine(b.dtype);
Console.WriteLine(a * b);
Console.ReadKey();
}
}
}
Why Use It
pythonnet offers significant advantages for developers working in both Python and .NET ecosystems. It enables seamless interoperability, allowing you to reuse existing codebases and libraries across languages. Python developers can access the vast array of .NET libraries and frameworks, while .NET developers can integrate Python's powerful scripting capabilities, data science tools, and machine learning libraries directly into their applications. This flexibility fosters greater productivity and expands the possibilities for complex application development.
Links
For more detailed information, documentation, and community support, refer to the following official resources: