0

I'm having issues trying to load a Dll's class made in c# with pythonnet

Here's the dll project config (VS)

enter image description here

Here's the class code

using System;

namespace ClassLibrary1
{
    public class Class1{   
        public void test()
        {
            Console.WriteLine("Hello from a library");
        }
    }
}

Here's the dll info

enter image description here

And the script

import clr
clr.AddReference('ClassLibraryAss')
from ClassLibraryAss import Class1
x = Class1()
x.test()

Is able to find the assembly. I follow some examples and it can't be more difficult than this.

This is the output

Traceback (most recent call last):
  File ".\testDLL.py", line 23, in <module>
    from ClassLibraryAss import Class1
ModuleNotFoundError: No module named 'ClassLibraryAss'

I tried different ways to load the module but with no luck

Environment

  • Pythonnet version: 2.5.2 (Also tried 2.5.1)
  • Python version: 3.8.0
  • Operating System: Windows 10
  • Visual studio 2019 (I didn't tried other)

3 Answers3

0

In your from XXX import Class1 XXX should be the namespace where Class1 resides, not the DLL name

LOST
  • 2,956
  • 3
  • 25
  • 40
0

Turns out that the dll was made in .net core and pythonnet by default don't work with that.

Acording to the issue

So making the dll with .net framework works. In the issue says a way to make this run with net core dlls, I didn't try it though

import clr
#I made a dll with .net Framework 4.8 called ClassLibraryNetF 
from ClassLibraryNetF import Class1

x = Class1()
x.test()
0

For anyone stumbling on this question while debuggin similar problem, pythonnet wouldn't load the assembly correctly if your script is being ran as a module, e.g. python -m Package.Module and inside you've added assembly folder to sys.path

sys.path.append(os.path.join(os.getcwd(), "Assemblies"))
clr.AddReference("MyAssembly")

import MyAssembly
from MyAssembly import SimpleClass

Don't know why, but the latest version reproduces this on Win 10, python 3.9. Probably something to do with the way pythonnet works with modules.

if you run it as a script from it's folder (python Module.py), it works fine.