2

I added a dll using pythonnet to my Python script like this:

import os
import clr
clr.AddReference(os.path.join(os.path.abspath('.'), 'dlls', 'Supplier.Bundle.dll'))
import Supplier.Bundle

It works just fine, I can call methods or instantiate classes right from the Supplier.Bundle namespace, but when I want to call a method from a nested namespace like Supplier.Bundle.Features I got the error:

AttributeError: Features

I also tried the following:

import Supplier.Bundle.Features

Which throws:

ModuleNotFoundError: No module named 'Supplier.Bundle.Features'; 'Supplier.Bundle' is not a package

Using the same dll in C# works just fine:

using Supplier.Bundle.Features

So my question is: how to access classes and methods of nested namespaces of a C# dll within Python3.x using pythonnet?

denfromufa
  • 5,610
  • 13
  • 81
  • 138
Szabolcs
  • 3,990
  • 18
  • 38

2 Answers2

2

Managed to solve the problem. Actually the Supplier.Bundle.dll had two other dependencies, so after copying the missing dlls to the project root, the import became error free.

Additionally the pythonnet package should've thrown more specific exceptions about the problem. That would've made the troubleshooting even more easier.

More on the issue: https://github.com/pythonnet/pythonnet/issues/516

Szabolcs
  • 3,990
  • 18
  • 38
1

I think I know a workaround for it. Use from module import. See the following example:

import clr
clr.AddReference('System.Windows.Forms')
#import System.Windows.Forms                # <-- not working
from System.Windows.Forms import MessageBox # <-- working

MessageBox.Show('Hello World')

Hope this helps.

Wollmich
  • 1,616
  • 1
  • 18
  • 46