Skip to content

Importing from DLL works only when namespace != assembly name #2418

@mardukbp

Description

@mardukbp

Environment

  • Pythonnet version: 3.0.3
  • Python version: 3.11.9
  • Operating System: 10.0.19045
  • .NET Runtime: 8.0.301

Details

I compiled the following code to CalcTest.dll with target framework netstandard2.0:

namespace CalcTest
{
    public static class Calculator
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }
    }
}

Then I tried importing the class Calculator from Python:

import sys
from pathlib import Path

sys.path.append(str(Path(__file__).parent / "DLLs"))

import clr

clr.AddReference("CalcTest")

from CalcTest import Calculator

and I obtained the following error:

ImportError: cannot import name 'Calculator' from 'CalcTest' (unknown location)

Following the tutorial How To Call A Dynamic Library I changed the namespace in C# to CalcTestNS, recompiled the project and updated the import in Python:

from CalcTestNS import Calculator

and it worked.

In the tutorial it is not mentioned that the namespace and the assembly cannot have the same name. It would be a great time-saver to document this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions