No way to call .net core 6.0 dll from python
See original GitHub issueEnvironment
- Pythonnet version: 3.0.1
- Python version: 3.11.0
- Operating System: Debian GNU/Linux 11 (bullseye)
- .NET Runtime: .net core 6.0
Details
Goal: attempt to call a simple static function in a .net core dll from a python script
The C# project contains only one file:
namespace Math
{
public class Calculator
{
public static int Factorial(int number)
{
int i;
int fact = 1;
for (i = 1; i <= number; i++)
{
fact *= i;
}
return fact;
}
}
}
The python script is the following:
from pythonnet import load
load("coreclr")
import clr
reference = clr.AddReference('./Math/bin/Debug/net6.0/publish/Math')
print(reference)
from Math import Calculator
factorial = Calculator.Factorial(6)
print(factorial)
- If there was a crash, please include the traceback here.
# python factorial.py
Math, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Traceback (most recent call last):
File "factorial.py", line 8, in <module>
from Math import Calculator
ImportError: cannot import name 'Calculator' from 'Math' (unknown location)
To reproduce the environment, here’s a dockerfile:
FROM python:3.11.0-slim-bullseye
# Install .Net
RUN apt-get update
RUN apt-get -y install wget
RUN wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN rm packages-microsoft-prod.deb
RUN apt-get update && \
apt-get install -y dotnet-sdk-6.0
RUN pip install pythonnet
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1
Issue Analytics
- State:
- Created 10 months ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Accessing .net core dll using python
I need to access a C# dll which is built in .net core (.NETCore version = v1.1). I tried in the below way...
Read more >Using a .NET library (DLL) in python/C++
I have a group of DLL libraries from an application, not developed by me. These libraries can function, as references, in a Visual...
Read more >How to Combine .NET Core and Python for Seamless ...
In this example, we're calling the Exec method to run a simple Python script that prints “Hello World from Python!” to the console....
Read more >.NET Core Support · Issue #984 · pythonnet ...
NET into Python. Each of them has a distinct "support" status for .NET Core. The first case should already be possible, as Python.NET ......
Read more >Anyway to invoke .net core dll from python? : r/dotnetcore
IronPython doesn't support libraries like pandas, matplotlib etc. Any way to call dotnet core dll from CPython?
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The issue is that you have a
Math
sub folder in the directory that you runfactorial.py
from. Python’s namespace package loader will take precedence over ours and the respective namespace package is of course empty. Rename theMath
directory to something else and it will work:I hate name collisions 😅 , thanks for saving me a headache!