Trying to run Python Code on .NET using Ironpython

162 Views Asked by At

I am trying to run a simple python code using C# using Iron Python Package.

Why it throws the below error (attached in screenshot)

The code runs fine if i don't import libraries,

but it throws error after importing libraries all tough i have stored required libraries in defined folders

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

enter image description here

why i am getting error as

future feature is not defined:annotations

1

There are 1 best solutions below

1
kurwjan On

As 001 commented, you can only import annotations from __future__ in Python 3.7 and IronPython currently only implements Python 3.4 (as stated here: https://ironpython.net/) and Pandas officially supports only 3.9, 3.10 and 3.11. (also stated here: https://pandas.pydata.org/docs/getting_started/install.html).

You could use an older Pandas version from https://pypi.org/project/pandas/#history but I don't know if there are security risks and if the documentation exists.