将 VS Code 附加到 IronPython

问题描述 投票:0回答:1

我正在尝试将 VS Code 调试器附加到从 nuget 包托管 IronPython 的应用程序。这不起作用,因为 Visual Studio Code 返回此错误:

这是我的 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"

我在 tmpC25C.py 文件中放置了一个断点,并尝试附加到控制台上标识的进程。当我尝试附加到进程时。控制台写了一行“无法找到 python 模块。

此后,Visual Studio Code 超时。

c# ironpython
1个回答
0
投票

debugpy(VSCode 使用的 Python 调试器)不支持 IronPython

© www.soinside.com 2019 - 2024. All rights reserved.