简体   繁体   中英

Pythonnet importing class: "ModuleNotFoundError: No module named..."

I am importing a C# dll into python using pytho.net.

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.

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 .

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

load("coreclr")
import clr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM