簡體   English   中英

Iron Python System.TypeLoadException:無法從程序集“System.Core”加載類型“System.Runtime.CompilerServices.Closure”

[英]Iron Python System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.Closure' from assembly 'System.Core'

我現在正在使用 Iron Python 在 ZD7EFA19FBE7D3972FD26BFC6B7EE9A3B755DDZ 中運行基於 dll 的 python。

所以我的 C# 代碼中有這一行:

public void Runpython(string name, string id)
{
   var engine = Python.CreateEngine();    
   
   //Get Dll
   var path = Environment.CurrentDirectory;
   var fullPath = @$"{path}\PythonService.dll";
   engine.Runtime.LoadAssembly(Assembly.LoadFile(fullPath));
   
   //Get pythonservice.py file         
   var scope = engine.Runtime.ImportModule("pythonservice");

   //Get PythonService class
   var pythonService = scope.GetVariable("PythonService");

   //Run function
   var pyService = engine.Operations.CreateInstance(pythonService);
   var result = pyService.save(name, id);
            
   Console.WriteLine(result);
}

異常發生在這一行: var scope = engine.Runtime.ImportModule("pythonservice"); 當它試圖獲取我包裹在 dll 中的pythonservice.py文件時。

我能知道導致這種情況的問題是什么嗎?

我正在使用 Iron Python 2.7.11 和我的 C# Class 庫和控制台應用程序都是 Z303CB0EFBE7D3972FD5ADB603CB0EFE7D3972ZD61 核心

謝謝你。

你可以在沒有 IronPython 的情況下做同樣的工作

在“C:\Program Files\Python39\python.exe”或任何 python 環境中安裝您的庫

試試這個:

public string Run(string scriptFilePatch, string args)
{
    var psi = new ProcessStartInfo();
    psi.FileName = @"C:\Program Files\Python39\python.exe"; // or any python environment

    psi.Arguments = $"\"{scriptFilePatch}\" {args}";

    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
    psi.StandardOutputEncoding = Encoding.UTF8;

    string errors = "", result = "";

    using (var process = Process.Start(psi))
    {
        result = process.StandardOutput.ReadToEnd();
        errors = process.StandardError.ReadToEnd();

    }
    StringWriter writer = new StringWriter();
    HttpUtility.HtmlDecode(result, writer);
    string decodedString = writer.ToString();

    return decodedString;
}

用法:

 Run("c:/code/download.py", "\"imageUrl\" \"fileName}"");

並通過此閱讀 python 中的 arguments

Python 代碼:

    import sys
    url = sys.argv[1] #arg {imageUrl} recived from c# code
    fileName = sys.argv[2] #arg {fileName} recived from c# code

並將數據返回到 c# 代碼,你應該使用這個:

b = any_object;
sys.stdout.buffer.write(bytearray(b,"utf-8"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM