c# - 使用 pythonnet 从 .Net Core 调用 python 脚本

标签 c# python .net .net-core python.net

我正在尝试让 pythonnet 在我在 Linux 上运行的 .Net Core 应用程序中工作。

我在我的 .Net Core 项目中引用了 Python.Runtime.dll(我从 nuget 获得)。

我的代码是:

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");
        }
    }
}

我收到这个运行时错误:

 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"

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

How do I run a py file in C#?

Call Python from .NET

更新:

我试过在Visual Studio中打开\pythonnet-master\src\runtime**Python.Runtime.csproj**看能不能编译成.Net或者.Core,但是只能编译成.Net framework . 我找到这篇文章“How to port from .net framework to .net standard” 这是我必须做的吗?

最佳答案

我终于通过使用 2.4.0 版的自编译 Python.Runtime.dll 获得了成功。创建工作 DLL 有两种选择:

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

对于选项 2,以下有效(在 Windows、Mac 和 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 中,将 ReleaseWinPY3 替换为 ReleaseMonoPY3 因为前者使用python37 后者使用python3.7)
  4. 在 Mac 中设置 DYLD_LIBRARY_PATH 或在 linux 中设置 LD_LIBRARY_PATH(Windows 跳过):
export DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.7/lib
  1. 使用构建的 DLL bin\netstandard2.0\Python.Runtime.dll 作为 Visual Studio .NET Core 项目中的 DLL 引用(我的目标是 netcoreapp2.2netcoreapp3.1 也测试正常),例如结合以下代码,
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);
                }
            }
        }
    }
}

关于c# - 使用 pythonnet 从 .Net Core 调用 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53628176/

相关文章:

python - 如何使用带有数据的数据库在 Django 中运行测试?

c# - 在 C#/WPF 中构建共享代码库的技巧/资源?

.net - Caliburn EventAggregator moq 验证 PublishOnUIThreadAsync 异步方法

c# - DLL 进程每天在同一时间启动以检查文件

c# - 自定义控件和必填字段验证器

javascript - 从代码后面将值传递给 javascript

javascript - django include_javascript/use_javascript 和类似的

Python条件基础打印颜色字符串

c# - Angular - WCF RESTful 服务 - 发送日期时间

.net - ADO.NET 是在 .net 中访问数据库的唯一本地方式吗?