简体   繁体   English

使用 Pythonnet 在 C# 中嵌入 Python 脚本的问题

[英]Issues using Pythonnet to embed Python Script within C#

I'm trying to use the pythonnet nuget package to embed Python 3.6.5 (32-bit) on.Net 6.0 but it throws the following exceptions:我正在尝试使用pythonnet nuget package 来嵌入 Python 3.6.5(32 位),但它会在.Net 上抛出以下异常:

  • System.TypeInitializationException: 'The type initializer for 'Delegates' threw an exception.' System.TypeInitializationException:''Delegates' 的类型初始化程序引发了异常。'

  • DllNotFoundException: Could not load python36.dll. DllNotFoundException:无法加载 python36.dll。

  • Win32Exception: %1 is not a valid Win32 application. Win32Exception: %1 不是有效的 Win32 应用程序。

It worked previously on a 64 bit installation of python but due to some libraries I will be using I need to use a 32-bit installation.它以前在 python 的 64 位安装上工作,但由于我将使用一些库,我需要使用 32 位安装。

The code that throws these exceptions is:引发这些异常的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Python.Runtime; //Quantconnect.pythonnet V2.0.17


public class PyScripts
{
    public PyScripts()
    {

        Runtime.PythonDLL = "python36.dll";
        PythonEngine.Initialize();

    }
}

When I instantiate this class the exceptions are thrown.当我实例化这个 class 时,会抛出异常。

Solution Found: My C# platform target was set to 64-bit instead of 32-bit (x86), which explains why it worked with 64-bit Python and not with 32-bit.找到的解决方案:我的 C# 平台目标设置为 64 位而不是 32 位 (x86),这解释了为什么它适用于 64 位 Python 而不是 32 位。

https://learn.microsoft.com/en-us/visualstudio/ide/how-to-configure-projects-to-target-platforms?view=vs-2022 https://learn.microsoft.com/en-us/visualstudio/ide/how-to-configure-projects-to-target-platforms?view=vs-2022

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM