Harpreet Harpreet - 24 days ago 4x
C# Question

How to redirect output from python

I am new to IronPython. I want to use it in my C# application. Basically, I am trying to copy all the stdout from IronPython and get it in something like a string. I have followed this tutorial - https://blogs.msdn.microsoft.com/seshadripv/2008/07/08/how-to-redirect-output-from-python-using-the-dlr-hosting-api/

But, The MemoryStream object is always empty.

What could go wrong?


There are change in API scripts.

To use IronPython in c#:

1) Install the Nuget package

  Install-Package IronPython.StdLib
  Install-Package IronPython

2) To run your demo, modify your code:

  // ScriptRuntime runTime = ScriptRuntime.Create();  //create is not available
    ScriptRuntime runTime = ScriptRuntime.CreateFromConfiguration();

3) The using assembly:

  using IronPython.Hosting;
  using Microsoft.Scripting;
  using Microsoft.Scripting.Hosting;

4) Add the following to App.config:

   <?xml version="1.0" encoding="utf-8" ?>
  <section name="microsoft.scripting" type="Microsoft.Scripting.Hosting.Configuration.Section, Microsoft.Scripting"/>
  <language names="IronPython;Python;py" extensions=".py" displayName="IronPython" type="IronPython.Runtime.PythonContext,   IronPython"/>     

I tested the code in vs 2013 and it's working without errors.