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: