0

As the title says, I'm wanting to initiate an interactive shell instance for python within a Windows Form application.

.NET Runtime: .NET Framework 4.7.2 Python Version: 3.8.10 PythonNet Version: 3.0

Here is my C# Code:

using System;
using System.Windows.Forms;
using Python.Runtime;


namespace PythonInteractiveGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Runtime.PythonDLL = @"C:path\to\python38.dll";
            PythonEngine.Initialize();

            string command = System.IO.File.ReadAllText("script2.py");

            using (Py.GIL())
            {
                using (PyScope scope = Py.CreateScope())
                {
                    scope.Exec(command);
                }
            }
            PythonEngine.Shutdown();
        }
    }
}

And my Python code located within script2.py:

import code
variables = globals().copy()
variables.update(locals())
shell = code.InteractiveConsole(variables)
shell.interact()

The above results in an error:

  HResult=0x80131500
  Message=input(): lost sys.stdin
  Source=Python.Runtime
  StackTrace:
<Cannot evaluate the exception stack trace>

I'm thinking it has something to do with the lack of an available Console window but am unsure on how to proceed. This works when using the same .NET Framework, python, and Pythonnet version under a Console Applicaiton.

Thanks!

UPDATE: 07/06/2021 I was able to add a console window using the accepted answer to the following: How do I show a console output/window in a forms application?

This results in a separate issue that seems to confirm my suspicions: PythonException

  • Does a simple "HelloWorld" python script work without any issues? – Sal Jul 07 '21 at 03:12
  • Yes. The problem seems to be the usage of code.InteractiveConsole. – Chris Gambrell Jul 07 '21 at 03:30
  • Upon further investigation, it seems that it isn't due to the lack of a console window, but due to stdin not being available. I think it's possible to enable this using WINAPI but I'm still looking into it. – Chris Gambrell Jul 07 '21 at 13:57
  • Do you really need python to interact with some kind of stdin? Can't you take the input in your winforms application and then pass it to your python context? – Sal Jul 07 '21 at 14:27
  • I'm wanting to enable an interactive session for real-time debugging within our production code. This seemed like the most attractive option. – Chris Gambrell Jul 07 '21 at 14:32
  • Could this be relevant? https://stackoverflow.com/questions/2380649/redirect-python-standard-input-output-to-c-sharp-forms-application – Sal Jul 07 '21 at 14:58
  • Thanks for the link. It wasnt what I needed but it helped me find a solution that seems to work for my purposes! I wrote a Console application that uses PythonNet. It takes a Dictionary for arguments and contains a static method used for serializing it to bytes. The production code calls creates the arg dict and serializes it. Then I open the Console application in another process, deserialize the Dict, and then pass it to the interactive python instance, maintaining the .NET objects in the process. Thanks for the back and forth. Wouldnt have gotten there otherwise! – Chris Gambrell Jul 08 '21 at 03:07

0 Answers0