PyCharm Jupyter Notebook debugger gives immediate "Debugger disconnected" once an external function raises an exception.

Answered

Hi, 

I noticed a problem when using PyCharm to debug a jupyter notebook. When an imported function raise an exception, the debugger will never attach until you restart the kernel.

 

To reproduce, create `crash.ipynb` in PyCharm with the following content 

#%%
from crash_func import crash
#%%
crash()
#%%

Also create a `crash.py`

def crash():
raise ValueError()

When the `crash()` cell in the notebook is executed with debugger, it will run without problem for the first time. If you debug the cell again, the debugger prompts "Debugger disconnected" immediately without any extra information.

 

Screenshot is attached below. 

 

Environment Information:

Python version: Python 3.8.6
Installed packages:
Package Version
------------------------------ -------------------
ipykernel 5.4.2
ipython 7.19.0
ipython-genutils 0.2.0
ipywidgets 7.6.3
jupyter 1.0.0
jupyter-client 6.1.11
jupyter-console 6.2.0
jupyter-core 4.7.0
jupyterlab-pygments 0.1.2
jupyterlab-widgets 1.0.0
kiwisolver 1.3.1
nbclient 0.5.1
nbconvert 6.0.7
nbformat 5.1.2
notebook 6.2.0
pip 21.0
setuptools 49.6.0.post20210108
tornado 6.1
(irrelevant libraries were stripped out)

PyCharm information:

PyCharm 2020.3.3 (Professional Edition)
Build #PY-203.7148.72, built on January 26, 2021
Licensed to Yuanyi Wu
Subscription is active until December 27, 2021.
For educational use only.
Runtime version: 11.0.9.1+11-b1145.77 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1962M
Cores: 12
Registry: debugger.watches.in.variables=false
Non-Bundled Plugins: org.jetbrains.plugins.hocon, com.intellij.plugins.html.instantEditing, com.jetbrains.intellij.datalore , com.leinardi.pycharm.mypy, intellij-micropython, ru.meanmail.plugin.requirements
6
8 comments

Same thing here

0

Hello, 

 

Thank you for reporting this. I have submitted an issue https://youtrack.jetbrains.com/issue/PY-47721, please vote for it in order to be updated on the resolution. 

 

Sorry for the inconvenience. 

1
Avatar
Permanently deleted user

While not a fix for Ywu647's problem, this thread is the top of google when searching "pycharm jupyter notebook debugger disconnected", and I post this for posterity's sake:

What fixed it for me was that I was using a conda virtual environment with the wrong kernel (anaconda base environment).

What fixed it for me was running:

!source activate myenv
!python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

in a Jupyter Notebook cell (replace myenv with your environment name) and restarting PyCharm.

1

Hi, I've detected a pattern that triggers this bug: when you use a cell magic such as %%time a the top of the cell, it cannot be debbuged.

1

Remy Hosseinkhan: I experience the bug when using cell magic as well.

It also doesn't seem to be fixed with: https://youtrack.jetbrains.com/issue/PY-47721

0

Same problem, when a cell execution somehow crashed, I need to restart the Jupyter server for the debug to work.

0

Please make sure you use PyCharm 2021.2 or newer.

If so, report another issue to https://youtrack.jetbrains.com/issues/PY and provide code and steps for reproducing the problem.
We'll look into it.

0

Please sign in to leave a comment.