Skip to content

.NET Core Support #984

Closed
Closed

Description

@filmor

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.

Activity

  1. added this to the 2.5.0 milestone on Nov 8, 2019
  2. pinned this issue on Nov 8, 2019
  3. heartacker commented on Dec 30, 2019

    @heartacker

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

  4. deleted a comment from 7934345 on Mar 10, 2020
  5. ericjohannsen commented on Mar 26, 2020

    @ericjohannsen

    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?

  6. lostmsu commented on Mar 27, 2020

    @lostmsu
    Member

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

  7. ericjohannsen commented on Mar 27, 2020

    @ericjohannsen

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

  8. lostmsu commented on Mar 27, 2020

    @lostmsu
    Member

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

  9. removed this from the 2.5.0 milestone on Apr 23, 2020
  10. tig commented on Apr 27, 2020

    @tig

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

  11. lostmsu commented on Apr 27, 2020

    @lostmsu
    Member

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

  12. tig commented on Apr 27, 2020

    @tig

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

  13. exones commented on Jun 23, 2020

    @exones

    Hello everyone,

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

    Thanks !

  14. vivainio commented on Nov 3, 2020

    @vivainio
    Contributor

    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.

  15. renedacosta commented on Feb 9, 2021

    @renedacosta

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

  16. 7 remaining items

  17. tibortakacs commented on Feb 17, 2021

    @tibortakacs

    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.

  18. tibortakacs commented on Feb 18, 2021

    @tibortakacs

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

  19. deleted a comment from ingted on Mar 21, 2021
  20. zhen8838 commented on Nov 12, 2021

    @zhen8838

    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'
  21. anders-wind commented on Nov 12, 2021

    @anders-wind

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

  22. zhen8838 commented on Nov 12, 2021

    @zhen8838

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

  23. usama124 commented on Nov 16, 2021

    @usama124

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

  24. weixu02 commented on Nov 23, 2021

    @weixu02

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

  25. lostmsu commented on Nov 23, 2021

    @lostmsu
    Member

    @weixu02 we are targeting January-February 2022

  26. MCSmit27 commented on Jan 28, 2022

    @MCSmit27
  27. GSPP commented on Jan 28, 2022

    @GSPP

    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 ❤️

  28. filmor commented on Jan 28, 2022

    @filmor
    MemberAuthor

    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.

  29. locked as resolved and limited conversation to collaborators on Jan 28, 2022
  30. unpinned this issue on Jul 8, 2022
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