Skip to content Skip to sidebar Skip to footer

Strange Error While Using Pycharm To Debug Pyqt Gui

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 breakp

Solution 1:

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.

Solution 2:

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 name my code snippets code.py, unless otherwise constrained.

Python

According to [Python 3.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:\Work\Dev\StackOverflow\q042705279]> set py
PYTHONPATH=E:\Work\Dev\Utils

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

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\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\StackOverflow\q042705279]> echo. 2>code.py


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

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\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\\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 3.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:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.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
  • PyCharmIDE connects to that server in order to get output

Again, in my case it's:

"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/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

Post a Comment for "Strange Error While Using Pycharm To Debug Pyqt Gui"