Py.GIL() 在 C# 中给出错误 pythonnet embedded python

Py.GIL() is giving error pythonnet embedded python in C#

我有以下 C# 代码:

static void Main()
        {


            string pythonpath1 = @"C:\Users\user\Documents\pynet_test\Python\Python37";
            string pythonpath2 = @"C:\Users\user\Documents\pynet_test\Python\Python37\lib";
            string envpythonhome = @"C:\Users\user\Documents\pynet_test\Python\Python37\python37.dll";


            Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", envpythonhome, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PATH", pythonpath1, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONHOME", pythonpath1, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONPATH", pythonpath2, EnvironmentVariableTarget.Process);

            

            using (Py.GIL())
            {
                dynamic np = Py.Import("numpy");
                Console.WriteLine(np.cos(np.pi * 2));

                dynamic sin = np.sin;
                Console.WriteLine(sin(5));

                double c = np.cos(5) + sin(5);
                Console.WriteLine(c);

                dynamic a = np.array(new List<float> { 1, 2, 3 });
                Console.WriteLine(a.dtype);

                dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
                Console.WriteLine(b.dtype);

                Console.WriteLine(a * b);
            }

            Console.WriteLine(DateTime.Now);

        }

我得到的错误是:

System.MissingMethodException
  HResult=0x80131513
  Message=Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.
  Source=Python.Runtime
  StackTrace:
   at Python.Runtime.CodeGenerator..ctor()
   at Python.Runtime.DelegateManager..ctor()
   at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv, Boolean initSigs)
   at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv, Boolean initSigs)
   at Python.Runtime.PythonEngine.Initialize()
   at Python.Runtime.Py.GIL()
   at WrapperPython.Program.Main() in C:\Users\user\Documents\pynet_test\pynet_test\Program.cs:line 50

Python 环境在 Project 文件夹中,规格如下:

python 使用的版本:3.7 (x64)

python网络版本:2.5.2-cp37-cp37m-win_amd64

OS : Windows 服务器 2019

已参考站点打包下的python.Runtime.dll。 CSproj 如下所示:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
      <LangVersion>latest</LangVersion>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="Python.Runtime">
      <HintPath>..\Python\Python37\Lib\site-packages\Python.Runtime.dll</HintPath>
    </Reference>
  </ItemGroup>


</Project>

我已经尝试了所有在线提供的方法,但似乎找不到问题所在。我有一种预感,它基于环境变量,但不确定如何进行。

好吧,所以 python.net 安装确实没有很好的记录,维护 python.net 存储库的人并没有太大帮助,因为它不是一个“支持论坛”。

我通过安装 python.runtime.AllPlatflorms nuget 包并将环境变量指向正确的 python 文件夹/文件解决了这个问题。

这也适用于 python3.8。