我在我的C#-应用程序中成功地实现了IronPython。我将所有脚本存储在数据库中,并在需要时加载它们。现在我想用PTVS调试我的Python代码。但是,当我试图将远程调试器连接到我的应用程序时,visual总是说我应该使用ptvsd.enable_attach()。
我不明白这一点,我试了很多次,但没有什么真正的工作。
编辑:我可以知道如何使用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挂起,程序就不再有反应了。
刷新-按钮:

也许有人能拦住我,谢谢!
发布于 2014-03-12 13:14:39
假设进程在localhost上运行,并且您已经调用了ptvsd.enable_attach(),这可能是防火墙问题。您可能需要调整Windows防火墙以允许连接到该端口(我认为本地主机连接总是允许的,但我不确定)。
发布于 2014-03-26 07:42:22
伙计,您应该读取attach_server.py并在其中插入一些日志输出,特别是server_thread_func()。查看它,并将调试输出放在从启动连接到附加的某些点上。找到失败的地方,你就会明白原因。现在你可以修好它了。
还将调试输出添加到visualstudio_py_util.py::write_bytes()等中,您将了解向调试套接字发送和接收到/来自调试套接字的内容。
https://stackoverflow.com/questions/22336460
复制相似问题