简体   繁体   English

Pytho.net 导入 class:“ModuleNotFoundError:没有名为...的模块”

[英]Pythonnet importing class: "ModuleNotFoundError: No module named..."

I am importing a C# dll into python using pytho.net.我正在使用 pytho.net 将 C# dll 导入 python。

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. DLL 存在于 assemblydir 中。

Inside TFSHygiene: 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我遵循了这个问题的答案: 尝试使用 Python.NET 导入 c# dll 时出现“No module named”错误

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.内置 .NET 5.0 目标。

I never got it to work with .NET 5.0.我从来没有让它与 .NET 5.0 一起工作。 For me it worked with .NET Framework 4.8.对我来说,它适用于 .NET Framework 4.8。 See my answer for more details .有关更多详细信息,请参阅我的答案

For .net core, you need to add load("coreclr") before import clr :对于 .net 核心,您需要在import clr之前添加load("coreclr")

load("coreclr")
import clr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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