You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each of them has a distinct "support" status for .NET Core.
The first case should already be possible, as Python.NET can be compiled as a .NET Standard DLL that can be referenced within .NET Core.
The second case is actively being worked at in #857, I'll open a corresponding PR this weekend. There is a previous attempt in #612 that uses the older (in fact second) coreclr hosting API and significantly complicates the build-process, which is already annoyingly complicated.
Furthermore, we have to simplify the way how libpython is loaded in the embedding case, which @lostmsu has already tried out and we'll look into further once the new loading mechanism has landed.
When I create a .NET Standard 2.0 library project in VS2019 and add a NuGet reference to pythonnet_35_dotnet, I get this error:
Error NU1202
Package pythonnet_py35_dotnet 2.3.0 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package pythonnet_py35_dotnet 2.3.0 does not support any target frameworks.
I tried changing the project's .NET Standard version to both 1.6 and 1.0 and received a similar error message.
@ericjohannsen you can do that right now if you build from source. But the NuGet package is very outdated. Current release is 2.4.1, and the nuget is for 2.3.0. We are working on releasing a new NuGet in a month or two.
Reacted by ericjohannsen, Huzaif Abdul-Sattar and heartacker
@tig try setting Runtime.PythonDLL to full path to python38.dll. I warn you though, that Python 3.8 is not officially supported in that package either. I suggest you use 3.7 if possible.
We are interested in case 2, "Embedding .NET into Python". In our planning, it would be very useful to know whether this work is "indefinitely blocked" or merely taking a break. Particularly interested in using this from dotnetcore and Linux.
Reacted by Sebastien Frigon, Tommi Pehkonen, Jari Lehtinen, Staffan Gustafsson, Jarmo Reponen, Burak Büyükatlı, morganics, ivan-crd, Neil Mosafi, Mikhail Barg and 2 more
@filmor,
I'm also looking to embed .net core assembly in my python code and see that this development is not yet integrated into the pythonnet package. I was wondering if the new clr loader from https://github.com/pythonnet/clr-loader can be used as a workaround to load assemblies from .net core for now ? if so is there an example on how to import namespaces correctly?
You are right, I was not accurate in my description. This is really not a self-contained application but a library collection with standalone .NET runtime. This solution is a workaround for my original goal to load libraries from a self-contained application that is not yet supported by .NET Core: dotnet/runtime#35329 (Starting a self-contained application is possible but in the case of Python.NET, I believe, it is not an option.)
Thank you for the clear explanation, this was my understanding. From the user's point of view, this could be slightly confusing since clr is a pure Python module (clr.py) and a module that is created by PythonNET Python extension, and now it is marked as "legacy loader". It is not a big issue, and I know it is challenging to resolve this (especially due to backward compatibility), this is just a minor remark. Potentially, some comment in the clr.py and/or in pythonnet could resolve this.
When I install pythonnet but still not find pythonnet module, Can some one help me?
I use m1 Mac book, dotnet 6.0.0. When pip install pythonnet, It build with mono, Then I import clr will got error:
❯ python
Python 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:00:30)
[Clang 11.0.1 ] on darwin
Type "help", "copyright", "credits" or "license"for more information.
>>> import clr
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
ImportError: dlopen(/Users/lisa/mambaforge/lib/python3.9/site-packages/clr.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '_mono_assembly_get_image'
So I want to change the runtime lib, but I can't import the pythonnet:
>>> from clr_loader import get_coreclr
rt = get_coreclr("/Users/lisa/Documents/nncase/runtimeconfig.json")
from pythonnet import set_runtime
set_runtime(rt)>>> rt = get_coreclr("/Users/lisa/Documents/nncase/runtimeconfig.json")
['/usr/local/share/dotnet/host/fxr/6.0.0/libhostfxr.dylib']
>>>
>>> from pythonnet import set_runtime
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
ModuleNotFoundError: No module named 'pythonnet'
from clr_loader import get_coreclr from pythonnet import set_runtime rt = get_coreclr("app.runtime.json") set_runtime(rt) import sys sys.path.append(r"/home/usama/Music/net5.0") import clr clr.AddReference(r"DLI") from DLI import Class1 calc = Class1() print(calc.__class__.__name__) result = calc.HelloWorld() print(result)
But when I tried to load .NET 5.0 DLL I got following errors in import clr
Traceback (most recent call last): File "/home/usama/PycharmProjects/DotNetSDK/main.py", line 10, in <module> import clr File "/home/usama/.local/lib/python3.8/site-packages/pythonnet-3.0.0.dev1-py3.8.egg/clr.py", line 6, in <module> load() File "/home/usama/.local/lib/python3.8/site-packages/pythonnet-3.0.0.dev1-py3.8.egg/pythonnet/__init__.py", line 42, in load func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"] File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/wrappers.py", line 38, in __getitem__ return self.get_function(name) File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/wrappers.py", line 35, in get_function return ClrFunction(self._runtime, self._path, name, func) File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/wrappers.py", line 16, in __init__ self._callable = runtime.get_callable(assembly, typename, func_name) File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/hostfxr.py", line 79, in get_callable check_result(res) File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/util/__init__.py", line 26, in check_result raise error clr_loader.util.clr_error.ClrError: 0x80131509: COR_E_INVALIDOPERATION => An operation is not legal in the current state.
I successfully tested this code on Windows OS with same pythonnet package and same DLL. but on Linux it is not working.
Can anyone please help on this?
@filmor@anders-wind
Thanks again for the great work here. It's been a while since last release, and I am wondering when will there be a stable release with .net core support? Also is .net 6 supported currently? Thanks!
The issue is closed because support has been merged into master. We don't have the time to track released features separately right now, we might do that once 3.0 is out.
Activity
heartacker commentedon Dec 30, 2019
Come here and see every few days, hoping to use it as soon
ericjohannsen commentedon Mar 26, 2020
When I create a .NET Standard 2.0 library project in VS2019 and add a NuGet reference to pythonnet_35_dotnet, I get this error:
Error NU1202
Package pythonnet_py35_dotnet 2.3.0 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package pythonnet_py35_dotnet 2.3.0 does not support any target frameworks.
I tried changing the project's .NET Standard version to both 1.6 and 1.0 and received a similar error message.
What am I missing?
lostmsu commentedon Mar 27, 2020
@ericjohannsen
.NET Standard is not currently supported. If you are fine with a temporary unsupported solution, try https://www.nuget.org/packages/Python.Runtime.UnofficialNetStandard/Latest Python.NET 3.0 previews support .NET Standard, .NET Core, and .NET 5+
ericjohannsen commentedon Mar 27, 2020
Thank you for that pointer. Why, though, does the original comment state, "Python.NET can be compiled as a .NET Standard DLL"?
lostmsu commentedon Mar 27, 2020
@ericjohannsen you can do that right now if you build from source. But the NuGet package is very outdated. Current release is 2.4.1, and the nuget is for 2.3.0. We are working on releasing a new NuGet in a month or two.
tig commentedon Apr 27, 2020
This requires Python3.7 (tries to load python37.dll). Any chance of getting an update that works with 3.8?
Or simple instructions on how to build src for dotnet, because current .sln files don't have that in it.
lostmsu commentedon Apr 27, 2020
@tig try setting
Runtime.PythonDLLto full path topython38.dll. I warn you though, that Python 3.8 is not officially supported in that package either. I suggest you use 3.7 if possible.tig commentedon Apr 27, 2020
That worked! Thanks. This is just a PoC so I'm good with the not official status for now. Thanks!
exones commentedon Jun 23, 2020
Hello everyone,
Is .net core support in active implementation phase? When would it be possible to expect a release?
Thanks !
vivainio commentedon Nov 3, 2020
We are interested in case 2, "Embedding .NET into Python". In our planning, it would be very useful to know whether this work is "indefinitely blocked" or merely taking a break. Particularly interested in using this from dotnetcore and Linux.
renedacosta commentedon Feb 9, 2021
@filmor,
I'm also looking to embed .net core assembly in my python code and see that this development is not yet integrated into the pythonnet package. I was wondering if the new clr loader from https://github.com/pythonnet/clr-loader can be used as a workaround to load assemblies from .net core for now ? if so is there an example on how to import namespaces correctly?
7 remaining items
tibortakacs commentedon Feb 17, 2021
You are right, I was not accurate in my description. This is really not a self-contained application but a library collection with standalone .NET runtime. This solution is a workaround for my original goal to load libraries from a self-contained application that is not yet supported by .NET Core: dotnet/runtime#35329 (Starting a self-contained application is possible but in the case of Python.NET, I believe, it is not an option.)
Thank you for the clear explanation, this was my understanding. From the user's point of view, this could be slightly confusing since
clris a pure Python module (clr.py) and a module that is created by PythonNET Python extension, and now it is marked as "legacy loader". It is not a big issue, and I know it is challenging to resolve this (especially due to backward compatibility), this is just a minor remark. Potentially, some comment in theclr.pyand/or inpythonnetcould resolve this.tibortakacs commentedon Feb 18, 2021
(I have found a bug with virtual environment: #1388)
zhen8838 commentedon Nov 12, 2021
When I install pythonnet but still not find pythonnet module, Can some one help me?
I use m1 Mac book, dotnet 6.0.0. When pip install pythonnet, It build with mono, Then I import clr will got error:
So I want to change the runtime lib, but I can't import the pythonnet:
anders-wind commentedon Nov 12, 2021
@zhen8838 The currently released version of pythonnet (2,5,2) does not support dotnet yet.
zhen8838 commentedon Nov 12, 2021
@anders-wind Thanks, Now I successfully use dotnet core in Mac m1 (python 3.9) from master branch, Hope Pythonnet can make new stable release.
usama124 commentedon Nov 16, 2021
@filmor @anders-wind
I successfully installed Pythonnet from master branch on my Linux system (UBUNTU 18.04) using following commands:
sudo apt-get install clangsudo apt-get install libglib2.0-devsudo apt install gitsudo apt install nugetmono /usr/lib/nuget/nuget.exe config -set http_proxy=http://my.proxy.address:port/usr/bin/python3 -m pip install -U pycparser --user/usr/bin/python3 -m pip install -U git+https://github.com/pythonnet/pythonnet --user --eggI used following app.runtime.json file to load .NET 5.0 DLL
{"runtimeOptions": {"tfm": "net5.0","framework": {"name": "Microsoft.NETCore.App","version": "5.0.2"}}}`
and here is the code I used to load .NET 5.0 DLL
from clr_loader import get_coreclrfrom pythonnet import set_runtimert = get_coreclr("app.runtime.json")set_runtime(rt)import syssys.path.append(r"/home/usama/Music/net5.0")import clrclr.AddReference(r"DLI")from DLI import Class1calc = Class1()print(calc.__class__.__name__)result = calc.HelloWorld()print(result)But when I tried to load .NET 5.0 DLL I got following errors in import clr
Traceback (most recent call last):File "/home/usama/PycharmProjects/DotNetSDK/main.py", line 10, in <module>import clrFile "/home/usama/.local/lib/python3.8/site-packages/pythonnet-3.0.0.dev1-py3.8.egg/clr.py", line 6, in <module> load()File "/home/usama/.local/lib/python3.8/site-packages/pythonnet-3.0.0.dev1-py3.8.egg/pythonnet/__init__.py", line 42, in load func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"]File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/wrappers.py", line 38, in __getitem__ return self.get_function(name)File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/wrappers.py", line 35, in get_function return ClrFunction(self._runtime, self._path, name, func)File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/wrappers.py", line 16, in __init__ self._callable = runtime.get_callable(assembly, typename, func_name)File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/hostfxr.py", line 79, in get_callable check_result(res)File "/home/usama/.local/lib/python3.8/site-packages/clr_loader/util/__init__.py", line 26, in check_result raise errorclr_loader.util.clr_error.ClrError: 0x80131509: COR_E_INVALIDOPERATION => An operation is not legal in the current state.I successfully tested this code on Windows OS with same pythonnet package and same DLL. but on Linux it is not working.
Can anyone please help on this?
weixu02 commentedon Nov 23, 2021
@filmor @anders-wind
Thanks again for the great work here. It's been a while since last release, and I am wondering when will there be a stable release with .net core support? Also is .net 6 supported currently? Thanks!
lostmsu commentedon Nov 23, 2021
@weixu02 we are targeting January-February 2022
MCSmit27 commentedon Jan 28, 2022
GSPP commentedon Jan 28, 2022
Should this issue really be closed given that it has not been delivered yet?
PythonNet is a super helpful library. Many thanks to the team for providing it ❤️
filmor commentedon Jan 28, 2022
The issue is closed because support has been merged into master. We don't have the time to track released features separately right now, we might do that once 3.0 is out.