0

I have little to no experience with C# but I'm attempting to import a C# class into a python script for a project I'm working on but I constantly run into a pylint(import-error).

Originally, I thought that the issue was due to compiling .Net Core instead of .Net Framework as pointed at in this post; however, I'm still running into the same import error after compiling with .Net Framework 4.7.2.

The C# class I am trying to import is the same as the aforementioned post (built under the ClassLibrary(.Net Standard) type):

TestClassLibrary.cs

using System;
namespace TestClassLibrary
{
    public class MyClass
    {
        public string function()
        {
            return "Hello World!";
        }
    }
}

Python script that I'm trying to import the C# class into:

import sys
import clr
sys.path.append(r"<Ablsloute Path to \bin>\Debug\netstandard2.0")
clr.AddReference(r"TestClassLibrary")
from TestClassLibrary import MyClass

To provide more context:

  • I'm using Python 3.8.1 with PythonNet 2.5.1
  • The C# solution was built successfully and the .dll file is created (using VS 2019)

I noticed some responses to similar questions suggested that the directory should be added to the python path but I don't think that is the issue I'm running into. As a sanity check, I created a python file in the same directory as the .dll file which contained a class/function that I was able to successfully call in my script.

1 Answers1

0

I do not think Python.NET probes Python's sys.path when searching for C# assemblies. Instead, .NET logic is used. On Windows you can simply add the path to DLL directory to PATH environment variable.

LOST
  • 2,956
  • 3
  • 25
  • 40