VS Code调试器连接IronPython宿主应用超时及模块缺失问题求助
无法将VS Code调试器附加到IronPython托管应用,超时错误
尝试将VS Code调试器附加到通过NuGet包托管IronPython的应用时失败,VS Code返回错误:"Timed out waiting for debug server to connect."
附加进程时,控制台输出"Unable to find python module.",随后出现超时。
Script.cs 文件
using System; using System.Collections.Generic; using System.ComponentModel; using IronPython.Hosting; using Microsoft.Scripting.Hosting; namespace PythonEngine { /// <summary> /// Represents a python script file /// </summary> public class Script : INotifyPropertyChanged, INotifyPropertyChanging { #region Fields private readonly object m_lock = new object(); private static readonly ScriptEngine s_debuggerEngine; private static readonly ScriptEngine s_runtimeEngine; private bool m_Debug = false; private string m_Filename = string.Empty; #endregion #region Properties /// <summary> /// Gets or sets whether to allow debugging of script files. /// </summary> public bool Debug { get { return m_Debug; } set { if (m_Debug == value) return; PropertyChanging?.Invoke(this, new(nameof(Debug))); m_Debug = value; PropertyChanged?.Invoke(this, new(nameof(Debug))); } } /// <summary> /// Gets or sets the file path and name to the script file. /// </summary> public string Filename { get { return m_Filename; } set { if (value == null) value = string.Empty; if (m_Filename == value) return; PropertyChanging?.Invoke(this, new(nameof(Filename))); m_Filename = value; PropertyChanged?.Invoke(this, new(nameof(Filename))); } } /// <summary> /// Gets the scope of available to/from the script. /// </summary> public Dictionary<string, object> Scope { get; } = new(); #endregion #region Constructors /// <summary> /// Initialize static variables. /// </summary> static Script() { Dictionary<string, object> debugOption = new() { {"Debug", true }, }; Dictionary<string, object> runOption = new() { {"Debug", false }, }; s_debuggerEngine = Python.CreateEngine(debugOption); s_runtimeEngine = Python.CreateEngine(runOption); } #endregion #region Methods /// <summary> /// Executes this script object. /// </summary> /// <returns><see langword="true"/> if the script was successfully executed; otherwise, <see langword="false"/>.</returns> public bool Execute() { return Execute(out _); } /// <summary> /// Executes this script object. /// </summary> /// <param name="exception">Any exceptions that occurs in the script.</param> /// <returns><see langword="true"/> if the script was successfully executed; otherwise, <see langword="false"/>.</returns> public bool Execute(out Exception exception) { exception = null; try { ScriptEngine engine; if (Debug) engine = s_debuggerEngine; else engine = s_runtimeEngine; lock (m_lock) { ScriptScope scope = engine.CreateScope(Scope); engine.ExecuteFile(Filename, scope); } return true; } catch (Exception e) { exception = e; return false; } } #endregion #region Delegates, Events, Handlers [field: NonSerialized] public event PropertyChangingEventHandler PropertyChanging; [field: NonSerialized] public event PropertyChangedEventHandler PropertyChanged; #endregion } }
Program.cs 文件
using PythonEngine; using System; using System.Collections.Generic; using System.Diagnostics; namespace PythonConsoleTest { internal class Program { static void Main(string[] args) { string user = Environment.GetEnvironmentVariable("USERNAME"); string filename = String.Format(@"C:\Users\{0}\Downloads\_PY Test\tmpC25C.py", user); Script script = new Script() { Debug = true, Filename = filename, }; script.Scope["globals"] = new Dictionary<string, string>() { { "working_directory", "Something" }, }; string[] arguments = new string[] { filename }; Console.WriteLine("Console Process ID: " + Process.GetCurrentProcess().Id); Console.WriteLine("Current File: " + arguments[0]); Console.WriteLine("Paused to allow VS Code Debugging"); Console.WriteLine("Press any key to continue"); Console.ReadKey(); Console.WriteLine(); script.Execute(); Console.WriteLine("Python file executed"); Console.WriteLine(); Console.ReadKey(); } } }
tmpC25C.py 文件
if globals["working_directory"] == "Something": globals["working_directory"] = "Something Else"
内容的提问来源于stack exchange,提问作者user8934733
相关产品推荐
相关产品推荐

