0

是否可以在 PyCharm 中远程调试诅咒程序?如何设置?

我遵循 PyCharm 4.0.8 指令,将其添加到EXAMPLE.py来自“”的“ npyscreen-4.8.7”中。

import pydevd
pydevd.settrace('localhost', port=8899, stdoutToServer=False, stderrToServer=True)

并且总是在“ setupterm”中遇到错误:

$ PYTHONPATH=~/bin/pycharm-debug.egg python EXAMPLE.py
Traceback (most recent call last):
  File "EXAMPLE.py", line 34, in <module>
    App.run()
  File "/home/.../npyscreen-4.8.7/npyscreen/apNPSApplication.py", line 30, in run
    return npyssafewrapper.wrapper(self.__remove_argument_call_main)
  File "/home/.../npyscreen-4.8.7/npyscreen/npyssafewrapper.py", line 41, in wrapper
    wrapper_no_fork(call_function)
  File "/home/.../npyscreen-4.8.7/npyscreen/npyssafewrapper.py", line 83, in wrapper_no_fork
    _SCREEN = curses.initscr()
  File "/usr/lib64/python2.6/curses/__init__.py", line 33, in initscr
    fd=_sys.__stdout__.fileno())
_curses.error: setupterm: could not find terminal

问题是 pydevd 将环境“TERM”从“xterm”更改为“emacs”。这可以通过一个小测试程序来验证。

import pydevd
pydevd.settrace('localhost', port=8899, stdoutToServer=False, stderrToServer=True)
import os as _os
import sys as _sys
import curses
print " my term: ", _os.environ.get("TERM", "unknown"), "\n"
print " my fd:   ", _sys.__stdout__.fileno(), "\n"
print "\n  ok  1  \n"
curses.setupterm(term=_os.environ.get("TERM", "unknown"),
#curses.setupterm(term='xterm',
                fd=_sys.__stdout__.fileno())
print "\n  ok  2  \n"
  • 如果删除带有“ pdevd”的两行,则程序成功。TERM它打印出来的“”是“ ” xterm
  • 如果将“”的第一个参数更改setupterm为“ term='xterm'”,即使使用“pydevd ”也成功。

我想问题是如何让“ pydevd”设置正确的“ TERM”?

4

2 回答 2

2

指出 pydevd 中 TERM 的硬编码设置的各种提示引导我进入这个序列,这对我有用:

#
# From the instructions in the PyCharm remote debug setup screen,
# add this to your code before curses is initialised.
#
import pydevd
pydevd.settrace('localhost', port=8899)
#
# And then to fixup the broken setting in pydevd...
#
os.environ['TERM'] = 'xterm'
于 2016-09-18T12:18:52.307 回答
1

虽然答案之一可能是:更改/usr/lib/python2.6/curses/__init__.py为强制 TERM,例如:

def initscr():
    import _curses, curses
    # we call setupterm() here because it raises an error
    # instead of calling exit() in error cases.
    _os.environ['TERM'] = 'xterm' ##hack force 'xterm' for pydevd debugging.
    setupterm(term=_os.environ.get("TERM", "unknown"),
              fd=_sys.__stdout__.fileno())
    stdscr = _curses.initscr()
    for key, value in _curses.__dict__.items():
        if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'):
            setattr(curses, key, value)

    return stdscr

更改“”的原始来源TERM在这里:

$ head plugins/org.python.pydev_4.0.0.201504132356/pysrc/pydev_ipython_console.py 
import sys
from pydev_console_utils import BaseInterpreterInterface

import os

os.environ['TERM'] = 'emacs' #to use proper page_more() for paging


# Uncomment to force PyDev standard shell.
# raise ImportError()
于 2015-05-08T02:32:16.320 回答