首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用pythonnet从.Net核心调用python脚本

使用pythonnet从.Net核心调用python脚本
EN

Stack Overflow用户
提问于 2018-12-05 08:39:31
回答 2查看 11.5K关注 0票数 6

我试图让pythonnet在运行在Linux上的.Net核心应用程序中工作。

我在我的Python.Runtime.dll核心项目中引用了努基特(这是从努基特获得的)。

我的代码是:

代码语言:javascript
复制
using System;
using Python.Runtime;
namespace pythonnet_w
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start");
            using (**Py.GIL()**) {
            //     blabla
            }
            Console.WriteLine("End");
        }
    }
}

我得到这个运行时错误:

代码语言:javascript
复制
 Unhandled Exception: System.MissingMethodException: Method not found:     'System.Reflection.Emit.AssemblyBuilder      
 System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.
    at Python.Runtime.CodeGenerator..ctor()
    at Python.Runtime.DelegateManager..ctor()
    at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv)
    at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv)
    at Python.Runtime.PythonEngine.Initialize()
    at Python.Runtime.Py.GIL()
    at pythonnet_w.Program.Main(String[] args) in D:\Development\~.Net libraries (3.part)\phytonnet\.Net Core test (phytonnet)\c#\pythonnet_test\Program.cs:line 10
 /usr/sbin/pythonnet_w: line 5: 19487 Aborted                 dotnet "/usr/share/pythonnet_wit/pythonnet_w.dll"

试图在这些线程中找到解决方案,但没有任何结果:

如何在C#中运行py文件?

从.NET调用Python

更新:

我试图在Visual中打开\pythonnet-master\src\runtime**Python.Runtime.csproj**,看看是否可以将其编译为.Net或.Core,但只能编译到.Net框架。我发现这篇文章"如何从.net框架移植到.net标准“是我必须做的吗?

EN

回答 2

Stack Overflow用户

发布于 2019-05-10 22:58:30

通过使用2.4.0版本的自编译Python.Runtime.dll,我终于取得了成功。创建工作DLL有两个选项:

  • 从相应的项目文件中删除其他目标框架net461 (只保留netstandard2.0)。
  • 使用适当的选项运行dotnet build

对于选项2,下面的工作(在Windows和Linux中):

  1. 克隆pythonnet (https://github.com/pythonnet/pythonnet)
  2. 在pythonnet文件夹中,cd src\runtime
  3. 在Windows中运行dotnet build -c ReleaseWinPY3 -f netstandard2.0 Python.Runtime.15.csproj (在Mac/Linux中,用ReleaseMonoPY3替换ReleaseWinPY3,因为前者使用python37,后者使用python3.7)
  4. 在Mac或linux中设置DYLD_LIBRARY_PATHLD_LIBRARY_PATH (Windows):
代码语言:javascript
复制
export DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.7/lib
  1. 在Visual .NET核心项目中使用已构建的DLL .NET作为DLL引用(.NET netcoreapp2.2netcoreapp3.1也测试好),例如结合以下代码,
代码语言:javascript
复制
using System;
using Python.Runtime;

namespace Python_CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Py.GIL())
            {
                dynamic os = Py.Import("os");
                dynamic dir = os.listdir();
                Console.WriteLine(dir);

                foreach (var d in dir)
                {
                    Console.WriteLine(d);
                }
            }
        }
    }
}
票数 6
EN

Stack Overflow用户

发布于 2020-06-14 22:52:15

您可以在您的IronPython应用程序中托管.NET解释器。例如,使用NuGet,您可以下载正确的包,然后将脚本执行(实际上是IronPython引擎)嵌入到应用程序中。

参考文献:https://medium.com/better-programming/running-python-script-from-c-and-working-with-the-results-843e68d230e5

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53628176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档