Strange error while using Pycharm to debug PyQt gui

Question:

I’ve been using PyCharm to debug my gui in PyQt. This has been really successful thus far, until I’ve run into a strange error in trying to debug my gui just now. I’ve set a breakpoint at the beginning of the script as well as at various points but the program does not have a chance to get to this point. I’ve also tried removing all the breakpoints and running the debug but get the same result. The full traceback is:

C:UserspbreachContinuumAnaconda3python.exe "C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.3helperspydevpydevd.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)JetBrainsPyCharm Community Edition 2016.3helperspydev_pydevd_bundlepydevd_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)JetBrainsPyCharm Community Edition 2016.3helperspydevpydevconsole.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)JetBrainsPyCharm Community Edition 2016.3helperspydevpydevd.py", line 26, in <module>
    from _pydevd_bundle.pydevd_additional_thread_info import PyDBAdditionalThreadInfo
  File "C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.3helperspydev_pydevd_bundlepydevd_additional_thread_info.py", line 17, in <module>
    from _pydevd_bundle.pydevd_cython_wrapper import PyDBAdditionalThreadInfo
  File "C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.3helperspydev_pydevd_bundlepydevd_cython_wrapper.py", line 26, in <module>
    mod = __import__(check_name)
  File "_pydevd_bundlepydevd_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)JetBrainsPyCharm Community Edition 2016.3helperspydev_pydevd_bundlepydevd_frame.py", line 10, in <module>
    from _pydevd_bundle.pydevd_breakpoints import get_exception_breakpoint
  File "C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.3helperspydev_pydevd_bundlepydevd_breakpoints.py", line 15, in <module>
    from _pydevd_bundle.pydevd_comm import get_global_debugger
  File "C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.3helperspydev_pydevd_bundlepydevd_comm.py", line 75, in <module>
    import pydevconsole
  File "C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.3helperspydevpydevconsole.py", line 10, in <module>
    from _pydevd_bundle.pydevconsole_code_for_ironpython import InteractiveConsole
  File "C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.3helperspydev_pydevd_bundlepydevconsole_code_for_ironpython.py", line 105
    except SyntaxError, err:
                      ^
SyntaxError: invalid syntax

Process finished with exit code 1

Does anyone know what is causing this error and how it can be resolved? When I run the code normally (without debugging) I do not run into any of these errors.

Asked By: pbreach

||

Answers:

I ran into the same issue, and it took me a while but I found a solution that works for me. I believe what happens, is that the debugger is looking for the module _pydevd_bundle.pydevd_cython in a directory code. However, because you are running the script out of your own code directory, the debugger checks your folder, sees their is no module, and throws the error. That would explain why deleting the __init__.py works, because the debugger won’t confuse the two directories anymore.

So, renaming your code directory to something else, should fix the issue and let you keep the init file.

Answered By: Andrew McNally

I’ve recently hit this issue (while working on [SO]: zipfile.BadZipFile: Bad CRC-32 when extracting a password protected .zip & .zip goes corrupt on extract (@CristiFati’s answer)).
As a note, I used to name my code snippets code.py, unless otherwise constrained (now I use code00.py, as it’s more generic and I can increment the number at the end if I have more than one file).

Python

According to [Python.Docs]: Modules – The Module Search Path (emphasis is mine):

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.

Little demo:

[cfati@CFATI-5510-0:e:WorkDevStackOverflowq042705279]> set py
PYTHONPATH=E:WorkDevUtils

[cfati@CFATI-5510-0:e:WorkDevStackOverflowq042705279]> dir /b

[cfati@CFATI-5510-0:e:WorkDevStackOverflowq042705279]> "e:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.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:WorkDevStackOverflowq042705279]> echo. 2>code.py


[cfati@CFATI-5510-0:e:WorkDevStackOverflowq042705279]> dir /b
code.py

[cfati@CFATI-5510-0:e:WorkDevStackOverflowq042705279]> "e:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe"  -c "import code;print(code)"
<module 'code' from 'e:\Work\Dev\StackOverflow\q042705279\code.py'>

As seen (under normal circumstances), if the module code (applies to packages as well) is found in cwd, it’s loaded from there.
The code module (code.py) is not randomly chosen (read the next section). Check [Python.Docs]: code – Interpreter base classes for details about one part of the standard Python library.

PyCharm

When running a PyCharm configuration, PyCharm (simplified) launches project interpreter on the configured script. This works. In my case it’s:

"E:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe" "E:/Work/Dev/StackOverflow/q042705279/code.py"

However when debugging the same configuration, things are a bit more complicated. The simplified version:

  • A server is created (by pydevd) that executes the target script

  • PyCharm IDE connects to that server in order to get output

Again, in my case it’s:

"E:WorkDevVEnvspy_064_03.06.08_test0Scriptspython.exe" "C:Installx64JetBrainsPyCharm Community EditionAllVershelperspydevpydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 45931 --file "E:/Work/Dev/StackOverflow/q042705279/code.py"

The key point is somewhere at the beginning of pydevconsole.py (which is mentioned in the exception traceback):

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

So, it tries to load the InteractiveConsole, which (obviously) fails.
Apparently, the code module is specific to CPython. Its absence means that it another distribution (check [Python]: Alternative Python Implementations).
The IPython alternative (selected by default) contains scripts that are not Python 3 (syntactically) compliant (at the current point IronPython is at v2.7.9). Note that this happens before even reaching user code.
Switching the interpreter to Python 2 won’t help either, it will pass this point, but later it will also try to setup the interactive console, which will fail.

Conclusion

  • Don’t have any module / package named code in your sys.path (before Python‘s standard paths)

  • Apparently pydevd.py has other arguments, but a quick check didn’t reveal any possibility of using them to get around this issue

  • I was able to successfully debug my code.py file (from PyCharm), by changing the Working directory from configuration settings. But I consider this a (lame) workaround (gainarie), and for complex setups it might not even work

[SO]: PyCharm doesn’t recognize installed module (@CristiFati’s answer) might also contain some useful info.

Answered By: CristiFati