0

I am working on ASP.NET WebAPI Web Services and one of my requirement is to call a python script that outputs a JSON object and send it as web service response.

I have Installed IronPython 2.7.9 nuget package and my computer has Python 3.6

I addded IronPython nuget package in my project and tried the code from link https://gist.github.com/0x49D1/23196eb99b4c1f089b2033b6191e84e8

test.py

    import json
    data = {}
    data['1'] = 'value1'
    data['2'] = 'value2'
    json_data = json.dumps(data)
    print (json_data)

Exception IronPython.Runtime.Exceptions.ImportException {"No module named json"}

user7050297
  • 117
  • 1
  • 1
  • 11
  • Working with JSON is pretty easy in .net. Would the re-implementaton in c# be significant? – Crowcoder Feb 06 '19 at 01:41
  • Have a look at [this question](https://stackoverflow.com/q/44441574/468244) ... You will have to setup the search paths in the engine for it. Your Python 3.6 installation should not matter and you should be using a library matching the IronPython environment. – Simon Opelt Feb 06 '19 at 09:03

1 Answers1

0

you should use Pythonnet and Python.Include from NuGet. Code sample:

if (Python.Runtime.PythonEngine.IsInitialized == false) {
    Python.Runtime.PythonEngine.Initialize();
    Python.Runtime.PythonEngine.BeginAllowThreads();
}
using (Py.GIL()) {
    ///
    /// your code
    ///
    if (Python.Runtime.PythonEngine.IsInitialized == true) {
        Python.Runtime.PythonEngine.InitExt();
    }
}
deadshot
  • 8,881
  • 4
  • 20
  • 39