Leonardo
Leonardo

Reputation: 1901

ModuleNotFoundError when importing a .NET custom class in pythonnet

I'm trying to import a VERY SIMPLE custom C# class into Python using pythonnet. I've never used C# or VS, so It's probably some stupid mistake I'm doing.

I have got a solid C# code base (not written by me) that I want to drive using Python.

I have this C# class:

using System;

public class MyClass
{
    string text;

    public MyClass(string text)
    {
        this.text = text;
    }

    public void Write()
    {
        Console.WriteLine(text);
    }
}

In VS 2017, I've created a .NET Core Class Library project. It compiles fine and creates a MyClass.dll file.

Then I'm trying to import it in Python:

import sys

sys.path.append(r"C:\Users\myuser\source\repos\hello\MyClass\bin\Debug\netcoreapp2.1")

import clr

clr.FindAssembly(r"MyClass")

clr.AddReference('MyClass')

import MyClass

But I always get a "ModuleNotFoundError: No module named 'MyClass'" error.

Upvotes: 2

Views: 1997

Answers (1)

Leonardo
Leonardo

Reputation: 1901

Turns out I was compiling the DLL with .NET Core. Changing to .NET Framekwork 4.5 solved the problem.

I wish the error messages were more helpful.

Upvotes: 3

Related Questions