1

I have Visual Studio 2013 with my main form written in C#. And I have a python(3.7) script with many functions (using libraries like Pandas, Numpy etc.)

How do I call the Python functions from my C#.net code using iron python? i.e. on a particular event from .net UI, the python script should be called and return the value as a parameter from python to .net and vice versa

  1. Tried Iron python
  2. Powershell to call the python script from .net.

.Net Code:

using Microsoft.Scripting.Hosting;

using IronPython.Hosting;

using IronPython.Runtime.Types;

using IronPython.Modules;


ScriptEngine py = IronPython.Hosting.Python.CreateEngine();

ScriptScope scope = py.ExecuteFile("Square.py");

dynamic Excel1 = scope.GetVariable("Excel")();

Excel1.ReadExcel("C:\\filePath\\abc.xlsx");

Python Script:


class Excel:    

    def ReadExcel(self, Path):

         import pandas as pd

         df = pd.read_excel (Path)

         print (df)  

         return df

An unhandled exception of type 'IronPython.Runtime.Exceptions.ImportException' occurs in Microsoft.Dynamic.dll

Additional information: No module named pandas

6
  • Which version of IronPython are you using? IronPython 3 is not ready for use at the moment and using IronPython 2.7 to execute a python 3.7 script won't work. Commented Sep 5, 2019 at 9:14
  • could you please let me know which version of iron python , python, visual studio is compatible for the above scenario. Commented Sep 5, 2019 at 10:43
  • The only version of IronPython that is final is 2.7. Your scripts should be python 2.7 to be compatible. But keep in mind that python 2.7 will retire at the end of year. Commented Sep 5, 2019 at 11:13
  • please let me know which approach to take to achieve this requirement. Commented Sep 6, 2019 at 14:10
  • You can choose one of these options: 1. use IronPython 2.7 and rewrite your script against this version. 2. Call that existing python 3.7 script from your application without using IronPython. That works like calling any other executable or script. The main disadvantage is that passing values is much more complex because this script will run in its own process. 3. Use any other scripting language that works as expected. Commented Sep 9, 2019 at 4:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.