← 输入框|github.com|刷新
Skip to content

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

Closed

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.

Activity

  1. changed the title [-]i[/-] [+]Importing from DLL works only when namespace != assembly name[/+] on Jul 17, 2024
  2. locked and limited conversation to collaborators on Jul 17, 2024
  3. converted this issue into a discussion #2420 on Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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