Skip to content

.NET Core support and CoreCLR embedding - cross-platform API #96

Closed

Description

@den-run-ai

Looks like with CoreCLR the unmanaged exports "hack" for embedding CLR can be extended by cross-platform hosting API:

https://github.com/dotnet/coreclr/issues/1256

This would allow simple C/C++ code to embed CLR

Current state of .NET Core support:

#96 (comment)

Activity

  1. den-run-ai commented on Sep 6, 2015

    @den-run-ai
    ContributorAuthor

    Note that for .NET 4+ still have to use unmanaged exports.

  2. den-run-ai commented on Jul 22, 2016

    @den-run-ai
    ContributorAuthor

    apparently @lstratman has extensive experience porting Mono to CoreCLR embedding.

  3. lstratman commented on Jul 25, 2016

    @lstratman
    Contributor

    Thanks @denfromufa :).

    It's funny, I actually had a fork of this repo from a while back with the goal of looking into this. But, since my company has only a single customer that might want to integrate our .NET server application into their Python application on Linux and, even then, only at some point in the future, it's down towards the bottom of my priority list for development unfortunately. However, I'm happy to offer some technical guidance.

    I implemented basically this same functionality for Node.js for the Edge.js project and a lot of the code that I wrote there can probably be re-used or easily re-purposed for pythonnet. https://github.com/tjanczuk/edge/blob/master/src/CoreCLREmbedding/coreclrembedding.cpp#L192 contains the code that spins up the .NET Core CLR in any process. Towards the end of the method (https://github.com/tjanczuk/edge/blob/master/src/CoreCLREmbedding/coreclrembedding.cpp#L673), we get pointers to a bunch of .NET methods that serve as the managed end of the Node.js->.NET interop process. These function pointers are then invoked anytime that we need to call from Node.js to .NET. Implementations of those .NET methods are in https://github.com/tjanczuk/edge/blob/master/src/double/Edge.js/dotnetcore/coreclrembedding.cs and are included in the .NET Core application that's run from within Edge.js through a NuGet package that needs to be referenced (see https://github.com/tjanczuk/edge#how-to-specify-additional-clr-assembly-references-in-c-code).

    This works well for Edge.js because the interaction between Node.js and .NET is all encapsulated in functions in one language or the other. So to call between the two domains, you're always invoking functions, never creating variables, setting properties, etc. However for pythonnet, we're obviously more "integrated" into the Python syntax, so we can do things like create variables, set properties, etc. You can write more methods in the .NET end of the interop process to facilitate this, or try to stay in unmanaged code as much as possible by calling the .NET Core CLR native methods to do things like create variables and set properties. That would be the more performant, but potentially more difficult, route. This will have to be done through the ICLRRuntimeHost instance that you get back from calling coreclr_initialize(). It should theoretically be possible, but is left as an exercise to the interested reader.

    Anyway, that concludes my brain dump, I hope that it helps. Feel free to post questions and I'll do my best to answer them. At some point, I may have some time for actual development as well, but can't offer any promises.

  4. den-run-ai commented on Sep 30, 2016

    @den-run-ai
    ContributorAuthor

    @lstratman thank you for very detailed response, there is a question from MS if CoreCLR C-API is good enough, would you mind replying there? MS is considering standardizing C-API for .NET ecosystem.

    https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/#comment-138395

    Are the four methods (Initialize, Shutdown, Create_Delegate, Execute_Assembly), mentioned enough for embedding CoreCLR in pythonnet?

  5. lstratman commented on Oct 1, 2016

    @lstratman
    Contributor

    I've replied to that thread that those methods are sufficient. Looking further into the existing pythonnet code, I realized that it does all of the things that I mentioned earlier (create variables, set properties, index arrays, etc.) in managed code anyway, so once the "spinning up CoreCLR" code is integrated from Edge.js, pythonnet should just be able to re-use a lot of its existing codebase.

  6. den-run-ai commented on Oct 5, 2016

    @den-run-ai
    ContributorAuthor

    Another thing holding of CoreCLR port is Reflection.Emit used in pythonnet in one place:

    https://github.com/dotnet/corefx/issues/4491#issuecomment-189756092

    But we could possibly just do conditional compilation for this feature.

  7. den-run-ai commented on Oct 5, 2016

    @den-run-ai
    ContributorAuthor

    otherwise try ikvm.reflection.emit or mono.cecil

  8. var1ap commented on Nov 23, 2016

    @var1ap

    Hi, I started work toward Python.Net Core edition in this fork https://github.com/var1ap/pythonnet , stay tuned.

  9. den-run-ai commented on Dec 4, 2016

    @den-run-ai
    ContributorAuthor

    @var1ap this is great news, do you agree to transition from Zope to MIT license, if your port is successful?

  10. var1ap commented on Dec 5, 2016

    @var1ap

    @denfromufa of course agree.

  11. var1ap commented on Dec 5, 2016

    @var1ap

    Greate news!
    https://github.com/var1ap/pythonnet/commit/2b508fd3fda1f67db6785b03a409b9edd0e67686
    I managed to get pythonnet work under dotnet core 1.1(Win x64)

  12. var1ap commented on Dec 27, 2016

    @var1ap

    Also pythonnet successfully ported to dotnet core 1.1(Linux x64 Ubuntu 16.04) (in the same fork).
    This is just Proof of concept. So somebody should port this project to CoreCLR with good code quality required for production projects.

  13. dmitriyse commented on Jan 1, 2017

    @dmitriyse
    Contributor

    Hi guys! Pythonnet really works under dotnet core >= 1.0(NetStandard1.5). Thank you very much var1ap for your research. I continued to work on porting pythonnet to dotnet core in this repo https://github.com/dmitriyse/pythonnet/tree/coreclr. Also I plan to improve my binary distribution and avoid any dll load magic. Finally we should get good nuget based distribution.

  14. den-run-ai commented on Feb 22, 2017

    @den-run-ai
    ContributorAuthor

    @var1ap @dmitriyse can you prepare a pull request with compilation flags for coreclr? much like .NET/Mono flags that we have currently.

  15. 34 remaining items

  16. MatthiasKauer commented on Nov 25, 2018

    @MatthiasKauer

    Hi, I'm trying to test calling from dotnet core into CPython.
    If I understand @denfromufa correctly, that should be working.
    But how do I set it up? pip install pythonnet fails with the following message:

    Could not load file or assembly 'System.ComponentModel.Composition, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.

    Installing the source version with python setup.py build_ext --inplace or just executing mono tools/nuget/nuget.exe leads to the same error. My interpretation attempt: mono on my Linuxmint 18.2 system could be too old.

    I also tried installing via nuget but got (for framework netcoreapp2.0)

    error: Package 'pythonnet' is incompatible with 'all' frameworks in project '/home/matt/dvl/experiments/pythonnet-core/pythonnet-core.csproj'.

    Questions:

    • Is upgrading mono through their apt repository the way to go?
    • Does the status table from @denfromufa maybe reflect the current master but not what is currently released?
    • If I want to write dotnet around pythonnet do I still install it via pip not via nuget?
  17. den-run-ai commented on Nov 26, 2018

    @den-run-ai
    ContributorAuthor

    @MatthiasKauer current dotnet support is listed for master branch, mono should be installed according to official mono installation guide (see also .travis.yml), pip is primary channel for full installation, nuget support is limited right now, e.g. see #165.

  18. MrM40 commented on Dec 7, 2018

    @MrM40

    Sorry for my ignorance. I'm trying to get embedding to work. I've installed pythonnet from nuget (2.3.0-py27-dotnet). But this Python.Runtime.dll doesn't seem to be .Net Standard or -Core. Tried to compile the source code \pythonnet-master\src\runtime\Python.Runtime.csproj to Standard or Core but it seem to be a .Net 4.x. Also tried the Python.Runtime.dll in the Wheel package from here., same problem.
    Can someone point me in a direction on how to get my hands on a .Net Standard or -Core version of Python.Runtime.dll?
    I read a lot of places that the master branch support .Core....but I really don't know what to do with this information beside what I already tried?

  19. den-run-ai commented on Dec 7, 2018

    @den-run-ai
    ContributorAuthor

    @MrM40 .net core builds are in this solution:

    https://github.com/pythonnet/pythonnet/blob/master/pythonnet.15.sln

    But I recommend building from python side by passing the --xplat flag to setup.py:

    https://github.com/pythonnet/pythonnet/blob/master/appveyor.yml#L60

    You can also grab the builds/wheels from appveyor artifacts.

    It is not possible to keep all 3 major runtimes (.NET Framework, .NET Core, and Mono) in the same solution/project.

  20. MrM40 commented on Dec 8, 2018

    @MrM40

    Still running against a wall. I only see release/debug to Mono and Win in those projects?
    And I assume I need to compile the "Console.15" project, right
    By the way, why not target .NET Standard in these projects?

  21. MrM40 commented on Dec 9, 2018

    @MrM40

    Thanks :-)
    I'm running on Linux(Debian) and now I get this errir:

    Start Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'python27': The specified module or one of its dependencies could not be found. (Exception from HRESULT: 0x8007007E) at Python.Runtime.Runtime.Py_IsInitialized() at Python.Runtime.Runtime.Initialize(Boolean initSigs) in D:\Development\~.Net libraries (3.part)\phytonnet\2018-12-09\pythonnet-master\src\runtime\runtime.cs:line 274 at Python.Runtime.PythonEngine.Initialize(IEnumerable1 args, Boolean setSysArgv, Boolean initSigs) in \pythonnet-master\src\runtime\pythonengine.cs:line 169 at Python.Runtime.Py.GIL() in \pythonnet-master\src\runtime\pythonengine.cs:line 619 at pythonnet_w.Program.Main(String[] args) in \pythonnet_test\Program.cs:line 9 Aborted

    I made two links: python2.7 and python27 in the same folder as Python.Runtime.dll pointing to "/usr/bin/python2.7".

    Not I got this error:

    Start Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point named 'PyUnicodeUCS2_FromUnicode' in DLL 'python27'. at Python.Runtime.Runtime.PyUnicode_FromUnicode(String s, IntPtr size) at Python.Runtime.Runtime.Initialize(Boolean initSigs) in \pythonnet-master\src\runtime\runtime.cs:line 337 at Python.Runtime.PythonEngine.Initialize(IEnumerable 1 args, Boolean setSysArgv, Boolean initSigs) in pythonnet-master\src\runtime\pythonengine.cs:line 169 at Python.Runtime.Py.GIL() in \pythonnet-master\src\runtime\pythonengine.cs:line 619 at pythonnet_w.Program.Main(String[] args) in \pythonnet_test\Program.cs:line 9 Aborted

  22. den-run-ai commented on Dec 9, 2018

    @den-run-ai
    ContributorAuthor

    @MrM40 please don't sidetrack this .NET Core (CoreCLR) status with other issues. In a separate issue, document how you are building and how you are using pythonnet to make a reproducible case. Also search the issue tracker for duplicate issues before posting.

  23. emilmuller commented on Mar 12, 2019

    @emilmuller

    I built https://github.com/pythonnet/pythonnet/blob/master/pythonnet.15.sln with the following compilation symbols:

    XPLAT;;PYTHON3;PYTHON37;UCS4;NETSTANDARD;

    Running this in a .NET Core application on Windows 8.1 causes the program to fatally crash:

    The program '[11180] dotnet.exe' has exited with code -1073740791 (0xc0000409).

    Without any more information. I'm sure more information can be obtained, but I'm unsure how.

    Kind regards, Emil

  24. dmitriyse commented on Mar 12, 2019

    @dmitriyse
    Contributor

    Please try UCS2

  25. emilmuller commented on Mar 12, 2019

    @emilmuller

    @dmitriyse Thanks! Changed to UCS2, now getting:

    The program '[12304] dotnet.exe' has exited with code 255 (0xff).

  26. dmitriyse commented on Mar 12, 2019

    @dmitriyse
    Contributor

    Unfortunately, I haven't tested this solution widely. Win 8.1 setup hasn't been tested. So you need to dive deeply with VS debugging.

  27. emilmuller commented on Mar 12, 2019

    @emilmuller

    @dmitriyse Hah! I got it working :) I had this line hanging around:

    PythonEngine.PythonPath = @"C:\Users\E\AppData\Local\Programs\Python\Python37";

    Removing it fixed the issue. Even though I think the path is correct.

  28. filmor commented on Nov 8, 2019

    @filmor
    Member

    Closed in favour of #984.

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