Registry / serialization / pythonnet

pythonnet

JSON →
library3.1.0pypypi✓ verified 26d ago

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 pythonnet
INSTALL
IMPORT
SIG · PYTHONNET
P
pythonnet
serializationpythonv3.1.0
Install
2.0s avg
Import
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 20.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.0s · import 0.000s · 21MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

clr
import clr

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.

import pythonnet # It's crucial to load the .NET runtime explicitly before importing 'clr'. # Options: "coreclr" (modern cross-platform .NET), "netfx" (Windows .NET Framework), "mono" (Linux/macOS Mono). # The choice depends on your OS and installed .NET environment. # For this example, we'll try 'coreclr' first, then 'netfx' as a common fallback for Windows. try: pythonnet.load("coreclr") # For .NET Core / .NET 5+ SDK installed except RuntimeError: print("Could not load 'coreclr'. Trying 'netfx' (Windows only).") try: pythonnet.load("netfx") # For .NET Framework on Windows except RuntimeError: print("Could not load 'netfx'. Trying 'mono' (Linux/macOS fallback).") try: pythonnet.load("mono") # For Mono runtime except RuntimeError: print("Failed to load any .NET runtime. Ensure a compatible .NET SDK or Mono is installed.") import sys; sys.exit(1) import clr # Explicitly add a reference to a .NET assembly. 'System' is fundamental. clr.AddReference("System") # Now you can import types from the .NET System namespace from System import DateTime from System import Environment print(f"Current Date and Time from .NET: {DateTime.Now}") print(f".NET OS Version: {Environment.OSVersion.VersionString}") # Example of creating a .NET object from System.Collections.Generic import List my_list = List[str]() my_list.Add("Hello") my_list.Add(".NET") print(f"Items in .NET List: {', '.join(my_list)}")
Debug
Known issues
breakingPython.NET 3.x dropped implicit assembly loading. All .NET assemblies (except possibly some core ones implicitly handled) must now be explicitly loaded using `clr.AddReference('AssemblyName')` before their types can be imported.
fix
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.
affects: 3.0.0 and later
breakingIn Python.NET 3.x, the conversion behavior for .NET enums and interface return types has changed. .NET enums are no longer implicitly converted to integers, and methods returning interfaces might require explicit casting to their concrete implementation types.
fix
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.
affects: 3.0.0 and later
gotchaThe .NET runtime must be explicitly initialized using `pythonnet.load()` *before* `import clr`. Calling `import clr` first will implicitly load a default runtime, which might not be the desired one or could fail if not configured correctly.
fix
Always call `from pythonnet import load; load('your_runtime_type')` at the beginning of your script, ensuring it executes before any `import clr` statements.
affects: All 3.x versions
breakingPython.NET 3.0.0 dropped support for Python 2.x. It is only compatible with Python 3.7 and newer.
fix
Migrate your Python code to Python 3.7 or a later supported version.
affects: 3.0.0 and later
gotchaPython.NET 3.x no longer implicitly converts arbitrary Python objects to `System.Boolean`. This can lead to `TypeError` if you rely on Python's truthiness rules for .NET boolean contexts.
fix
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`.
affects: 3.0.0 and later
gotchaPython's objects are all reference types, while .NET distinguishes between value types (like `int`, `struct`) and reference types (like `class`). When a .NET value type is used in a context expecting a reference type, it might be 'boxed' into an object on the heap, leading to copies rather than direct modification if not handled carefully.
fix
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.
affects: All versions
Upgrade
Version history
3.1.0latest on PyPI · released May 23, 2026
Audit
Dependencies
.NET RuntimerequiredRequires a compatible .NET runtime (e.g., .NET Framework, .NET Core/.NET 5+, or Mono) to be installed on the system to function.
clr-loaderrequiredUnderlying library for loading CLR runtimes, though typically bundled with pythonnet.
Agent activity
19 hits · last 30 days
node
16
Resources
pythonnet — pip install pythonnet · libregistry