尝试使用 Ironpython 在 .NET 上运行 Python 代码

问题描述 投票:0回答:2

我正在尝试使用 Iron Python 包使用 C# 运行简单的 python 代码。

为什么会抛出以下错误(附在屏幕截图中)

如果我不导入库,代码运行良好,

但是导入库后它会抛出错误,我已将所需的库存储在定义的文件夹中

import pandas as pd
print("HIIII")
print(a+b)
using IronPython.Hosting;//for DLHE 
using Microsoft.Scripting; 
using Microsoft.Scripting.Hosting;//provides scripting abilities comparable to batch files 
using System; 
using System.Diagnostics; 
using System.IO; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading.Tasks; 
using IronPython.Runtime; 
using IronPython; 

class Hi {     
    private static void Main(string[] args) {
        var engine = Python.CreateEngine();
        var searchPaths = engine.GetSearchPaths();
        searchPaths.Add(@"C:\Users\nayan.nirvikar\source\repos\PythonCSharpIntegration\Lib");
        engine.SetSearchPaths(searchPaths);
        var scope = engine.CreateScope();
        scope.SetVariable("a", 10);
        scope.SetVariable("b", 20);

        var source = engine.CreateScriptSourceFromFile(@"C:\Users\nayan.nirvikar\Downloads\sample_script.py");
        var compilation = source.Compile();
        var result = compilation.Execute(scope);
        foreach (var varName in scope.GetVariableNames()){
            Console.WriteLine($"Variable: {varName}, Value: {scope.GetVariable(varName)}");
        }
    }
}

为什么我收到错误

未来功能未定义:注释

python c# ironpython
2个回答
0
投票

正如 001 所评论的,您只能在

Python 3.7
中从
annotations
导入 __future__,而 IronPython 目前仅实现 Python 3.4(如此处所述:https://ironpython.net/)和 Pandas官方仅支持3.93.103.11。 (这里也有说明:https://pandas.pydata.org/docs/getting_started/install.html)。

您可以使用 https://pypi.org/project/pandas/#history 中的较旧 Pandas 版本,但我不知道是否存在安全风险以及文档是否存在。


0
投票

您不能将 Pandas 与 IronPython 一起使用。 Pandas 就像 Numpy、Scipy、Matplotlib 等,依赖于 C 库,但与 C# 不太兼容。

请参阅此处此处

尝试使用 Pythonnet

© www.soinside.com 2019 - 2024. All rights reserved.