4

我已经在我的 C#-Application 中成功实现了 IronPython。我将所有脚本存储在数据库中,并在需要时加载它们。现在我想用 PTVS 调试我的 Python 代码。但是,当我尝试将远程调试器连接到我的应用程序时,Visual Studio 总是说我应该使用ptvsd.enable_attach().

  1. 我想如果我为我的 Python 引擎启用调试模式就足够了
  2. 如果我需要导入 ptvsd,我如何导入脚本(inimain、...)我应该把它们也放在我的数据库中吗?

我无法弄清楚这一点并且已经尝试了很多,但没有任何真正的工作。

编辑:我可以弄清楚如何使用 ptvsd,我必须“包含”ptvsd 模块:

//copied from: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0

string dir = Path.GetDirectoryName("C:\\Support\\Modules\\ptvsd");
ICollection<string> paths = myScriptEngine.GetSearchPaths();

if (dir != null && dir != "")
{
    paths.Add(dir);
}
else
{
    paths.Add(Environment.CurrentDirectory);
}

但现在我在 os.py 中得到一个错误:

未定义全局名称“statvfs_result”

在行中:

_copy_reg.pickle(statvfs_result, _pickle_statvfs_result,
    _make_statvfs_result)

编辑 2:似乎我可以忽略带有全局名称的错误消息。但现在我收到以下消息:

IronPython 必须以 -X:Tracing 和 -X:Frames 选项启动以支持 PTVS 远程调试。

编辑 3:我使用以下代码解决了跟踪和帧的错误:

        Dictionary<string, object> options = new Dictionary<string, object>();
        options["Debug"] = true;
        options["Tracing"] = true;
        options["Frames"] = true;
        myScriptEngine = Python.CreateEngine(options);

但是现在我遇到了下一个问题,我无法将 Visual Studio 附加到我的应用程序,我总是收到以下错误消息:

无法连接到“localhost:5678”上的远程 Python 进程。确保进程正在运行,并且已调用 ptvsd.enable_attach()-

编辑4: 我的python代码:

# -----------------------------------------------
# Framework-Root-Script
# This script is the main-framework script
#  Autor: BE
#  Date: 07.10.2013
# -----------------------------------------------

# --------------------------------------------
import sys
#import atexit
import ptvsd

ptvsd.enable_attach(None)
#ptvsd.wait_for_attach()

#
from System import *
from System.Windows import MessageBox
from System.Windows.Controls import Grid, MenuItem
from ESS.MS.Base import GlobalSettings
from ESS.MS.Framework.Core.TaskbarNotification import TaskbarNotificationManager
from ESS.MS.Framework.UIG.Mask import DynamicMaskManager
# --------------------------------------------

# --------------------------------------------
#<summary>
#Eine Instanz dieser Klasse wird automatisch mit
#dem Start des DocCenter Studios erstellt.
#</summary>
class StudioInstance:

    # --------------------------------------------
    # Declarations

    # --------------------------------------------

    # --------------------------------------------
    # Constructor
    def __init__(self): 
        pass 
    # --------------------------------------------

    # --------------------------------------------
    # Will be called before the Login-Window open
    def BeforeUserLogin(self):
        try:
            pass
        except:
            pass
    # --------------------------------------------

    # --------------------------------------------
    #<summary>
    #Wird ausgeführt, wenn der Login für einen Benutzer
    # Fehlschlägt
    #</summary>
    #<param Name="InputUserName">Eingegeber Benutzername</param>
    #<param Name="InputDomain">Eingegebene Domain<param>
    def LoginFailed(self, InputUserName, InputDomain):
        try:
            pass
        except:
            pass
    # --------------------------------------------

    # --------------------------------------------
    # Will be called if the Login-Process is complete
    def LoginComplete(self, UserName, Domain):
        try:
            # -------------------------------------------------------------------
            # Control auf das Tray-Icon setzten (Linksklick)
            # Mask = DynamicMaskManager.Singleton.GetMaskInstance("Win_DCC_Bediener", False)

            # grid = Grid()
            # grid.Children.Add(Mask.VisualElement)

            # TaskbarNotificationManager.Singleton.AddTrayPopupControl(grid)
            # -------------------------------------------------------------------

            # -------------------------------------------------------------------
            # Context-Menu einttrag auf das Tray-Icon setzten
            # test = MenuItem()
            # test.Header = "Hallo Welt"
            # TaskbarNotificationManager.Singleton.AddContextMenuItem(test)
            # -------------------------------------------------------------------
            pass
        except Exception, e:
            MessageBox.Show(e.ToString())
    # --------------------------------------------

    # --------------------------------------------
    # Will be called synchron with the UI (same thread)
    def SyncUpdate(self):
        try:
            pass
        except Exception, e:
            MessageBox.Show(e.ToString())
    # --------------------------------------------

    # --------------------------------------------
    # Will be called in a custom thread
    def AsyncUpdate(self):
        try:
            pass
        except:
            pass
    # --------------------------------------------

# --------------------------------------------

编辑 5 我想我现在可以附加到这个过程中。但是当我单击 Visual Studio 调试器窗口中的刷新按钮时,Visual Studio 挂断并且程序不再反应。

刷新按钮: 截图

也许有人可以帮助我,谢谢!

4

2 回答 2

1

假设该进程正在运行localhost并且您已调用ptvsd.enable_attach(),这可能是防火墙问题。您可能需要调整 Windows 防火墙以允许连接到该端口(我认为始终允许本地主机连接,但我不确定)。

于 2014-03-12T13:14:39.960 回答
0

伙计,您应该阅读 attach_server.py 并在其中插入一些日志记录输出,尤其是 server_thread_func()。查看它并将调试输出放在从开始连接到附加的某些点上。找出失败的地方,你就会明白一个原因。现在你可以修复它了。

还将调试输出添加到 visualstudio_py_util.py::write_bytes() 等,您将了解从调试套接字发送和接收的内容。

于 2014-03-26T07:42:22.070 回答