使用 Pycharm 调试 PyQt gui 时出现奇怪的错误

Strange error while using Pycharm to debug PyQt gui

我一直在使用 PyCharm 在 PyQt 中调试我的图形用户界面。到目前为止,这真的很成功,直到我 运行 刚才在尝试调试我的 gui 时遇到了一个奇怪的错误。我在脚本的开头以及各个点都设置了断点,但程序没有机会到达这一点。我也试过删除所有断点并 运行 调试但得到相同的结果。完整的追溯是:

C:\Users\pbreach\Continuum\Anaconda3\python.exe "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\pydevd.py" --multiproc --qt-support --client 127.0.0.1 --port 53720 --file C:/Users/pbreach/Dropbox/FIDS/cci/bluebook/code/input.py
Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\_pydevd_bundle\pydevd_cython_wrapper.py", line 2, in <module>
    from _pydevd_bundle.pydevd_cython import trace_dispatch, PyDBAdditionalThreadInfo
ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\pydevconsole.py", line 8, in <module>
    from code import InteractiveConsole
ImportError: cannot import name 'InteractiveConsole'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\pydevd.py", line 26, in <module>
    from _pydevd_bundle.pydevd_additional_thread_info import PyDBAdditionalThreadInfo
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\_pydevd_bundle\pydevd_additional_thread_info.py", line 17, in <module>
    from _pydevd_bundle.pydevd_cython_wrapper import PyDBAdditionalThreadInfo
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\_pydevd_bundle\pydevd_cython_wrapper.py", line 26, in <module>
    mod = __import__(check_name)
  File "_pydevd_bundle\pydevd_cython_win32_36_64.pyx", line 9, in init _pydevd_bundle.pydevd_cython_win32_36_64 (_pydevd_bundle/pydevd_cython_win32_36_64.c:21388)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\_pydevd_bundle\pydevd_frame.py", line 10, in <module>
    from _pydevd_bundle.pydevd_breakpoints import get_exception_breakpoint
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\_pydevd_bundle\pydevd_breakpoints.py", line 15, in <module>
    from _pydevd_bundle.pydevd_comm import get_global_debugger
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 75, in <module>
    import pydevconsole
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\pydevconsole.py", line 10, in <module>
    from _pydevd_bundle.pydevconsole_code_for_ironpython import InteractiveConsole
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\_pydevd_bundle\pydevconsole_code_for_ironpython.py", line 105
    except SyntaxError, err:
                      ^
SyntaxError: invalid syntax

Process finished with exit code 1

有谁知道导致此错误的原因以及如何解决?当我 运行 代码正常时(没有调试)我不会 运行 进入任何这些错误。

我 运行 遇到了同样的问题,这花了我一段时间,但我找到了适合我的解决方案。我相信会发生什么,调试器正在目录 code 中寻找模块 _pydevd_bundle.pydevd_cython。然而,因为你是 运行 你自己的 code 目录中的脚本,调试器会检查你的文件夹,发现它们没有模块,并抛出错误。这可以解释为什么删除 __init__.py 有效,因为调试器不会再混淆这两个目录。

因此,将您的代码目录重命名为其他名称应该可以解决问题并让您保留 init 文件。

我最近遇到了这个问题(在处理 时)。请注意,我将我的代码片段命名为 code.py,除非另有限制。

Python

根据[Python 3.Docs]: Modules - The Module Search Path重点是我的):

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

小演示:

[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q042705279]> set py
PYTHONPATH=E:\Work\Dev\Utils

[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q042705279]> dir /b

[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q042705279]> "e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe"  -c "import code;print(code)"
<module 'code' from 'c:\Install\x64\Python\Python\03.06.08\Lib\code.py'>

[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q042705279]> echo. 2>code.py


[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q042705279]> dir /b
code.py

[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q042705279]> "e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe"  -c "import code;print(code)"
<module 'code' from 'e:\Work\Dev\Whosebug\q042705279\code.py'>

如所见(在正常情况下),如果模块 code(也适用于包)在 cwd,它是从那里加载的。
code模块(code.py)不是随机选择的(阅读下一节)。检查 [Python 3.Docs]: code - Interpreter base classes 以获取有关标准 Python 库的一部分的详细信息。

PyCharm

运行一个PyCharm配置时,PyCharm(简化)在配置的脚本上启动项目解释器。这行得通。在我的例子中是:

"E:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" "E:/Work/Dev/Whosebug/q042705279/code.py"

但是当调试相同的配置时,事情就有点复杂了。简化版:

  • 创建了一个执行目标脚本的服务器(由 pydevd
  • PyCharm IDE 连接到该服务器以获得输出

同样,在我的例子中是:

"E:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" "C:\Install\x64\JetBrains\PyCharm Community Edition\AllVers\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 45931 --file "E:/Work/Dev/Whosebug/q042705279/code.py"

关键点pydevconsole.py开头的某处(异常回溯中提到的):

try:
    from code import InteractiveConsole
except ImportError:
    from _pydevd_bundle.pydevconsole_code_for_ironpython import InteractiveConsole

因此,它尝试加载 InteractiveConsole,但(显然)失败了。
显然,code 模块特定于 CPython。它的缺失意味着它是另一个分布(检查 [Python]: Alternative Python Implementations).
The IPython alternative (selected by default) contains scripts that are not Python 3 (syntactically) compliant (at the current point IronPython 位于 v2.7.9)。请注意,这甚至发生在到达用户代码之前。
将解释器切换到 Python 2 也无济于事,它会通过这一点,但稍后它还会尝试设置交互式控制台,这会失败。

结论

  • 在您的 sys.path 中没有任何名为 code 的模块/包(在 Python' s 标准路径)
  • 显然 pydevd.py 还有其他论点,但快速检查并没有发现使用它们来解决这个问题的任何可能性
  • 我能够成功调试我的 code.py 文件(来自 PyCharm ),通过更改配置设置中的 工作目录 。但我认为这是一个(蹩脚的)解决方法(gainarie),对于复杂的设置,它 甚至可能不起作用