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

Sebastian Schumann
  • 3,204
  • 19
  • 37
  • Which version of `IronPython` are you using? [IronPython 3](https://github.com/IronLanguages/ironpython3) is not ready for use at the moment and using IronPython 2.7 to execute a python 3.7 script won't work. – Sebastian Schumann Sep 05 '19 at 09:14
  • could you please let me know which version of iron python , python, visual studio is compatible for the above scenario. – Barsa Swain Sep 05 '19 at 10:43
  • The only version of [IronPython](https://ironpython.net/) 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](https://pythonclock.org/). – Sebastian Schumann Sep 05 '19 at 11:13
  • please let me know which approach to take to achieve this requirement. – Barsa Swain Sep 06 '19 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. – Sebastian Schumann Sep 09 '19 at 04:15
  • Possible duplicate of [Call Python function from c# (.NET)](https://stackoverflow.com/questions/35462175/call-python-function-from-c-sharp-net) – Ankit Das Sep 17 '19 at 08:06

0 Answers0