1

I need to access a C# dll which is built in .net core (.NETCore version = v1.1). I tried in the below way but I am getting the import error.

import clr

clr.AddReference("dllname") - No error

from dllname import *

Got Import Error exception saying no module named dllname.

Note: I tried in both Iron python and python both are giving me the same exception.

2 Answers 2

2

I'm trying on Fedora 29, using mono 5.18, python3.7 and netcore 3.0.100-preview-009812, and seems to work if you use absolute paths to resolve the netcore dll

import clr
import os

clr.AddReference(os.path.abspath('./bin/Debug/netstandard2.0/sample.dll'))
import sample
p = sample.Person(name='Peter')

netcore project was generated like that

dotnet new classlib -o sample

Person class

using System;

namespace sample
{
    public class Person
    {
            public string Name { get; set; }
    }
}

UPDATE

Based on data provided by @SMHP seems like an incompatibility between main .NET framework/mono (pythonnet runtime) and a library targeting .netcoreapp 2.0.

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your comment, i tried but didn't worked for me.
just to get more information. In which operating system are you trying? and what python version ? Can you try previous minimum sample ?
I just tried in windows 10 with python2.7 and netcore 2.2, works just fine. Can you give more information about the dll you're trying to load ? is it possible that exist some kind of conflict between another python module with the same name ?
i have installed pythonnet 2.3.0 to access the .net dll's, also installed .net core sdk version 2.2.105.i confirmed that there is no existing python module in that dll's name.i have got this dll from other team, i believe its built in 64 bit computer.
// xxxx, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null // Global type: <Module> // Architecture: AnyCPU (64-bit preferred) // Runtime: .NET 4.0 [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: TargetFramework(".NETCoreApp,Version=v2.1", FrameworkDisplayName = "")]
0

You get import error because you are using dllname in import statement. Instead of using dllname use namespace of the dll. P.S: don't use same name for dll and namespace it will throw an error while importing in python

import clr

clr.AddReference("dllname") - No error

from namespace import *

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.