3

I am importing a C# dll into python using pythonnet.

import sys
import clr
assemblydir = r"C:\Users\Nathan_Dehnel\source\repos\TFSHygiene\bin\Debug\net5.0-windows"
sys.path.append(assemblydir)
clr.AddReference("TFSHygiene")
from TFSHygiene import QueryExecutor

The DLL is present inside assemblydir.

Inside TFSHygiene:

namespace TFSHygiene
{
    public class QueryExecutor
    {
        ...
    }
}

I followed the answer in this question: "No module named" error when attempting to importing c# dll using Python.NET

However I get this error when building:

Traceback (most recent call last):
  File "C:\Users\Nathan_Dehnel\OneDrive - Dell Technologies\Documents\ADO TFS\ADO TFS\main.py", line 12, in <module>
    from TFSHygiene import QueryExecutor
ModuleNotFoundError: No module named 'TFSHygiene'

Built with .NET 5.0 target.

Nathan Dehnel
  • 133
  • 10

2 Answers2

0

I never got it to work with .NET 5.0. For me it worked with .NET Framework 4.8. See my answer for more details.

Roald
  • 2,459
  • 16
  • 43
0

For .net core, you need to add load("coreclr") before import clr:

load("coreclr")
import clr
Starnuto di topo
  • 3,215
  • 5
  • 32
  • 66