I’ve been using Pythonnet for quite some time but always against .NET Framework 4.*
With the recent release of .NET 5.0 I wanted to migrate my projects but I could not make it work for non-Framework versions (e.g. .NET Core 2.0, .NET Core 3.0, .NET 5.0)
Here is a very simple test I ran with .NET Framework 4.5:
namespace TestNet
{
public class Dummy
{
private string _name;
public Dummy(string name)
{
_name = name;
}
public string Hello(string name)
{
return $”Hello {name}, my name is {_name}”;
}
}
}
This produces TestNet.dll then I run the following Python code:
>>> import clr
>>> clr.AddReference(“TestNet”)
>>> from TestNet import Dummy
>>> d = Dummy(“Bob”)
>>> d.Hello(“John”)
‘Hello John, my name is Bob’
So everything works fine.
Now, the exact same C# code but targeting .NET Core or .NET 5.0 will give me:
>>> import clr
>>> clr.AddReference(“TestNet”)
>>> from TestNet import Dummy
Traceback (most recent call last):
File “
ModuleNotFoundError: No module named ‘TestNet’
My understanding is they are using CoreClr and not CLR and from Pythonnet website (https://pythonnet.github.io/):
Python.NET (pythonnet) is a package that gives Python programmers nearly seamless integration with the .NET 4.0+ Common Language Runtime (CLR)
Does anyone know if CoreClr is supported?
Thanks!
Env: Python 3.8/Windows 10
Okay, here\’s a comprehensive answer addressing the Pythonnet and .NET Core/5+ compatibility issue, along with explanations and solutions:\n\n**Best Answer:**\n\nYes, .NET Core and .NET 5+ are indeed supported by Pythonnet, but the setup and nuances differ significantly from .NET Framework. The key is understanding that Pythonnet uses a different loading mechanism for CoreCLR-based runtimes. The documentation can be misleading because it heavily emphasizes .NET Framework.\n\nHere\’s a breakdown of the problem, the solution, and important considerations:\n\n**Understanding the Issue**\n\n* **.NET Framework vs. .NET Core/5+:** .NET Framework (4.x) uses the older CLR, which has a different assembly loading mechanism compared to CoreCLR used by .NET Core and .NET 5+. `clr.AddReference` works differently between the two.\n\n* **`clr.AddReference` Behavior:** For .NET Framework, `clr.AddReference` often \”just works\” if the DLL is in the same directory or in the GAC (Global Assembly Cache). With .NET Core/5+, it\’s *much* stricter. It requires a proper assembly context and dependency resolution.\n\n* **Pythonnet and CoreCLR:** Pythonnet *does* support CoreCLR, but you need to explicitly tell it where to find the .NET runtime and how to resolve dependencies.\n\n* **`ModuleNotFoundError`:** The `ModuleNotFoundError` you\’re seeing doesn\’t necessarily mean Pythonnet *can\’t* load the assembly. It means it can\’t find the *module* or a dependency required by that module.\n\n**Solution: Using `clr.AddReference` and Explicit Paths**\n\nThe most reliable way to load assemblies from .NET Core/5+ is to provide the *full path* to the DLL. This helps the CoreCLR loader find the assembly and its dependencies.\n\n“`python\nimport clr\nimport os # Import the \’os\’ module\n\n# Assuming TestNet.dll is in the same directory as your Python script\ndll_path = os.path.abspath(\”TestNet.dll\”) # Get the absolute path\n\ntry:\n clr.AddReference(dll_path)\n from TestNet import Dummy # Import after adding the reference\n d = Dummy(\”Bob\”)\n print(d.Hello(\”John\”))\nexcept Exception as e:\n print(f\”Error: {e}\”)\n“`\n\n**Explanation:**\n\n1. **`import os`:** We need the `os` module to work with file paths.\n2. **`os.path.abspath(\”TestNet.dll\”)`:** This line constructs the *absolute path* to your `TestNet.dll` file. This is *crucial* for .NET Core/5+ to find the assembly reliably. You might need to adjust the path if the DLL is not in the same directory as your Python script.\n3. **`clr.AddReference(dll_path)`:** This line explicitly tells Pythonnet exactly where to find the assembly.\n4. **`from TestNet import Dummy`:** Import the class *after* adding the reference.\n5. **Error Handling:** Wrap the code in a `try…except` block to catch any exceptions during assembly loading or type resolution. This helps you diagnose problems.\n\n**Important Considerations and Troubleshooting**\n\n* **.NET Runtime Installation:** Ensure you have the correct .NET Core/.NET 5+ runtime installed on your system. The target framework of your C# project needs to be present on the machine where you are running the Python script.\n\n* **Target Framework in C# Project:** Double-check the target framework in your C# project file (`.csproj`). It should be something like `