Python.NET (pythonnet) is a package that provides nearly seamless integration between Python and the .NET Framework, .NET Core, and Mono runtime on Windows, Linux, and macOS. It allows Python programmers to script .NET applications or build entire applications using .NET services and components written in any CLR-targeting language (C#, VB.NET, F#, C++/CLI). The current version is 3.0.5 and it maintains an active release cadence with support for recent Python and .NET versions.
pip install pythonnetVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the .NET runtime, add a reference to a .NET assembly, and then import and use .NET types and objects directly from Python. It explicitly handles potential runtime loading issues by trying different runtime types.
Ensure `clr.AddReference('YourAssembly')` is called for every .NET assembly you intend to use. Place relevant assembly directories in `sys.path` if they are not in the GAC or application base.For enums, use enum members directly (e.g., `MyEnum.Option`) or the enum constructor (e.g., `MyEnum(42)`). For interfaces, use explicit conversion like `ConcreteType(interface_instance)` if you need to access members specific to the concrete type.
Always call `from pythonnet import load; load('your_runtime_type')` at the beginning of your script, ensuring it executes before any `import clr` statements.Migrate your Python code to Python 3.7 or a later supported version.
Explicitly convert Python objects to `bool` or `System.Boolean` before passing them to .NET methods expecting a boolean, or ensure the Python object itself is a `bool`.
Be aware of the distinction between .NET value and reference types when working with .NET objects in Python. If modifying a value type, ensure you are working with the correct instance or reassigning the modified value.