8000 .NET Core Support · Issue #984 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

.NET Core Support #984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
filmor opened this issue Nov 8, 2019 · 28 comments
Closed

.NET Core Support #984

filmor opened this issue Nov 8, 2019 · 28 comments

Comments

@filmor
Copy link
Member
filmor commented Nov 8, 2019

There are two major modes of using Python.NET:

  1. Embedding Python into .NET
  2. Embedding .NET into Python

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.

@heartacker
Copy link

Come here and see every few days, hoping to use it as soon

@ericjohannsen
Copy link

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
Copy link
Member
lostmsu commented 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
Copy link

Thank you for that pointer. Why, though, does the original comment state, "Python.NET can be compiled as a .NET Standard DLL"?

@lostmsu
Copy link
Member
lostmsu commented 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.

@lostmsu lostmsu removed this from the 2.5.0 milestone Apr 23, 2020
@tig
Copy link
tig commented Apr 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/

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
8000 Copy link
Member
lostmsu commented Apr 27, 2020

@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.

@tig
Copy link
tig commented Apr 27, 2020

That worked! Thanks. This is just a PoC so I'm good with the not official status for now. Thanks!

@exones
Copy link
exones commented Jun 23, 2020

Hello everyone,

Is .net core support in active implementation phase? When would it be possible to expect a release?

Thanks !

@vivainio
Copy link
Contributor
vivainio commented 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
Copy link
renedacosta commented 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?

@filmor
Copy link
Member Author
filmor commented Feb 10, 2021

@renedacosta Work is on its way, we'll support the "running .NET Core from Python" use-case soon on master.

@filmor
Copy link
Member Author
filmor commented Feb 14, 2021

.NET Core support is now merged into master. The API is not final, yet, but here is an example:

from clr_loader import get_coreclr
from pythonnet import set_runtime

rt = get_coreclr("path/to/your.runtimeconfig.json")
set_runtime(rt)

import clr

@anders-wind
Copy link
anders-wind commented Feb 17, 2021

.NET Core support is now merged into master. The API is not final, yet, but here is an example:

from clr_loader import get_coreclr
from pythonnet import set_runtime

rt = get_coreclr("path/to/your.runtimeconfig.json")
set_runtime(rt)

import clr

This is amazing! But I tried it out locally (windows 10, python 3.8.3) with build: https://ci.appveyor.com/project/pythonnet/pythonnet/branch/master/job/p8gu64su5snt30nt/artifacts and this runtime config (self-contained dll):

{
  "runtimeOptions": {
    "tfm": "netcoreapp3.1",
    "includedFrameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "3.1.3"
      },
      {
        "name": "Microsoft.AspNetCore.App",
        "version": "3.1.3"
      }
    ],
    "configProperties": {
      "System.GC.Server": false,
      "System.Runtime.TieredCompilation": true,
      "System.Runtime.TieredCompilation.QuickJit": false
    }
  }
}

I get the following error message when running rt = get_coreclr('my_dll/my_dll.runtimeconfig.json') :

Initialization for self-contained components is not supported
---------------------------------------------------------------------------
ClrError                                  Traceback (most recent call last)
<ipython-input-5-cc23d44b62c6> in <module>
----> 1 rt = get_coreclr('matlabinterop_netcore/Alipes.Data.MatlabInterop.runtimeconfig.json')

~\Downloads\foo\bar\clr_loader\__init__.py in get_coreclr(runtime_config, dotnet_root, properties)
     32         dotnet_root = find_dotnet_root()
     33
---> 34     impl = DotnetCoreRuntime(runtime_config=runtime_config, dotnet_root=dotnet_root)
     35     if properties:
     36         for key, value in properties.items():

~\Downloads\foo\bar\clr_loader\hostfxr.py in __init__(self, runtime_config, dotnet_root)
     13         self._dll = load_hostfxr(self._dotnet_root)
     14         self._is_finalized = False
---> 15         self._handle = _get_handle(self._dll, self._dotnet_root, runtime_config)
     16         self._load_func = _get_load_func(self._dll, self._handle)
     17

~\Downloads\foo\bar\clr_loader\hostfxr.py in _get_handle(dll, dotnet_root, runtime_config)
    102         encode(runtime_config), params, handle_ptr
    103     )
--> 104     check_result(res)
    105
    106     return handle_ptr[0]

~\Downloads\foo\bar\clr_loader\util\__init__.py in check_result(err_code)
     24             error = ClrError(hresult)
     25
---> 26         raise error

ClrError: 0x80008093: InvalidConfigFile

For none self-contained dll it works just fine :)

I hope this can be of help in debugging the solution :) Thanks for all the good work!

@filmor
Copy link
Member Author
filmor commented Feb 17, 2021

I haven't tested self-contained deployments yet, will do that now :)

@tibortakacs
Copy link

Great achievement, thank you for this. I have just tried the latest master with self-contained deployment, it works properly on my side. I use the .NET 5.0.2 runtime with this config:

{
  "runtimeOptions": {
    "tfm": "net5.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "5.0.2"
    }
  }
}

Additionally, I tested the AddReference function, it works with custom DLLs. Actually, the clr module is meant to be "legacy Python.NET loader for backwards compatibility" but I could use AddReference only through this module (clr.AddReference). Is there any other way or plan which aligns with the new, modern pythonnet.load() concept?

@filmor
Copy link
Member Author
filmor commented Feb 17, 2021

Well, this is not a self-contained deployment then. Self-contained means that the frameworks are listed under the includedFrameworks key in the runtime config and all the required DLLs (managed and unmanaged) are included. With this configuration you won't be able to use the package without an installed .NET Core runtime.

The clr module is a bit of a weird beast. Once pythonnet.load is called, the previous module is replaced by one that is created within the C# library which implements AddReference etc. What is "legacy" about the clr.py is that it defaults to a particular runtime (.NET Framework on Windows and Mono otherwise). I'm still thinking about whether I want to remove this before the 3.0 release or whether we at least introduce a warning. The clr module as such will likely stay, but it could be that it only becomes available after calling pythonnet.load explicitly.

@tibortakacs
Copy link

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.

@tibortakacs
Copy link

(I have found a bug with virtual environment: #1388)

@zhen8838
Copy link

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'

@anders-wind
Copy link

@zhen8838 The currently released version of pythonnet (2,5,2) does not support dotnet yet.

@zhen8838
Copy link

@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
Copy link

@filmor @anders-wind
I successfully installed Pythonnet from master branch on my Linux system (UBUNTU 18.04) using following commands:

sudo apt-get install clang
sudo apt-get install libglib2.0-dev
sudo apt install git
sudo apt install nuget
mono /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 --egg

I 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_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?

@weixu02
Copy link
weixu02 commented 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
Copy link
Member
lostmsu commented Nov 23, 2021

@weixu02 we are targeting January-February 2022

@MCSmit27

This comment has been minimized.

@GSPP
Copy link
GSPP commented Jan 28, 2022 A4D9

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
Copy link
Member Author
filmor commented 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.

@pythonnet pythonnet locked as resolved and limited conversation to collaborators Jan 28, 2022
@filmor filmor unpinned this issue Jul 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0