我已经成功地在我的C#应用程序中实现了IronPython。我将所有脚本存储在数据库中,并在需要时加载它们。现在我想使用PTVS调试我的Python代码。但每当我尝试连接到远程调试器时,Visual Studio都会说我应该使用
编辑: 我找出了如何使用ptvsd,我必须“包含”ptvsd模块:
但是现在我在os.py中遇到了一个错误:
但现在我遇到了下一个问题,我无法将Visual Studio连接到我的应用程序,我总是收到以下错误消息:
“无法连接到本地主机的 Python 远程进程 'localhost:5678'。请确保该进程正在运行,并已调用 ptvsd.enable_attach() -” 编辑4: 我的Python代码:
ptvsd.enable_attach()
。
- 我认为如果我启用Python引擎的调试模式就足够了
- 如果我需要导入ptvsd,我该如何导入脚本(ini,main,...)?我也应该将它们放在我的数据库中吗?
编辑: 我找出了如何使用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:看起来我可以忽略全局名称的错误信息。
但是现在我收到了以下消息:
编辑3:我通过使用以下代码解决了跟踪和帧的错误。必须使用-X:Tracing和-X:Frames选项启动IronPython才能支持PTVS远程调试。
Dictionary<string, object> options = new Dictionary<string, object>();
options["Debug"] = true;
options["Tracing"] = true;
options["Frames"] = true;
myScriptEngine = Python.CreateEngine(options);
但现在我遇到了下一个问题,我无法将Visual Studio连接到我的应用程序,我总是收到以下错误消息:
“无法连接到本地主机的 Python 远程进程 'localhost:5678'。请确保该进程正在运行,并已调用 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挂起,并且程序不再响应。
刷新按钮:
也许有人可以帮助我,谢谢!