Repository navigation
Kernel dies when using matplotlib to plot in notebook or save plot to image files #25530
Description
Environment data
- VS Code version: 1.105.0
- Jupyter Extension version (available under the Extensions sidebar): 2025.9.0
- Python Extension version (available under the Extensions sidebar): 2025.16.0
- OS (Windows | Mac | Linux distro) and version: Windows_NT x64 10.0.26120
- Python and/or Anaconda version: 3.14.0
- Type of virtual environment used (N/A | venv | virtualenv | conda | ...): NA
- Jupyter server running: Local
Expected behaviour
Expect the matplotlib can show the image and save the image to file.
Actual behaviour
The kernel died.
Steps to reproduce:
[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]
- Run the code.
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
This bug is very close to Issue microsoft/vscode-jupyter#9283. I have been experimenting with it.
I found that this bug seems to be related to the python environments(ms-python.vscode-python-envs) extension. The bug only occurs when the extension is enabled and activated(in the python.useEnvironmentsExtension setting), and when using an interpreter that is not a conda or a venv environment interpreter(such as the pixi environment in this case.).
The bug would also sometimes occur even when using a conda environment interpreter if "python-envs.defaultEnvManager": "ms-python.python:system" is set in the vscode workspace.
I also experiment with other matplotlib backends(agg, qtagg, tkagg and so on) and other python versions(3.12-3.14). These do not affect the outcomes.
However the bug would not happen in the jupyter notebook running in the browser, or using the jupyterhub extension(ms-toolsai.jupyter-hub) in vscode to connect to a running jupyter server.
Logs
Output for Jupyter in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Jupyter)
18:05:32.718 [warn] Failed to get activated env vars for d:\Academic\global_soil_moisture_pattern\sm_pattern_new\.pixi\envs\default\python.exe in 30178ms
18:05:32.782 [info] Process Execution: d:\Academic\global_soil_moisture_pattern\sm_pattern_new\.pixi\envs\default\python.exe -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
18:05:32.846 [info] Process Execution: d:\Academic\global_soil_moisture_pattern\sm_pattern_new\.pixi\envs\default\python.exe -Xfrozen_modules=off -m ipykernel_launcher --f=~\AppData\Roaming\jupyter\runtime\kernel-v324d4ded8293c9240fc9812540c55c92d907ccd53.json
> cwd: d:\test
18:05:34.131 [info] Restarted cd4f7a9a-e0ff-4c9e-aab2-971daca8e25d
18:05:34.711 [error] Disposing session as kernel process died ExitCode: 3228369023, Reason: traitlets.traitlets.TraitError
Activity
faultdiagnosistoolbox commentedon Oct 14, 2025
Edit:
After managing to dig deeper I found the problem I encountered and it was related to the update of ipykernel package to version 7.0.x. Downgrading to 6.30.1 resolves the issue.
Sorry for my interruption of this topic.
I think I have the same issue but I haven't been able to pinpoint exact versions of packages/extensions when it appeared.
The minimal example above excites the problem for me. If I run the code interactively with a Jupyter session, it plots nicely inline. But if I activate a matplotlib backend the session crashes when I plot (have tried the tk backend).
If I run directly in ipython; starts thebackend with %matplotlib and run the code it works as expected.
DonJayamanne commentedon Oct 15, 2025
Thank you for the informaiton
Zhu Liyang (@zhu181) Please could you try revering ipykernel as suggested.
Erik Frisk (@faultdiagnosistoolbox) Do you run into similar isues outside vs code in Jupyter lab as well? If you do I suggest filing an issue upstream.
faultdiagnosistoolbox commentedon Oct 15, 2025
Yes, tried it now and I experience exactly the same behavior in Jupyter lab.
I have reported the issue to ipykernel (I hope I got it in the right place in the issues system) ipython/ipykernel#1438 (comment)
zhu181 commentedon Oct 15, 2025
Don Jayamanne (@DonJayamanne) Reinstalling ipykernel doesn't fix this problem. Another minor issue is that the output panel doesn't always show kernel died reason. As the following shows
Erik Frisk (@faultdiagnosistoolbox) I am not sure if the problem you encounter is related to this one, as I do not experience this behavior after your example in ipython/ipykernel#1438 (comment).
Also, downgrading ipykernel to 6.30 doesn't fix this problem either, so I consider this to be a problem with the python environment extension. Perhaps I should file an issue there?
zhu181 commentedon Oct 15, 2025
Move the code to a .py file and find the same problem: matplotlib fail to plot image when python environment extension is enabled. This seems more like a problem with the python environment extension.
DonJayamanne commentedon Oct 15, 2025
Zhu Liyang (@zhu181) Do you run into this issue if you disable/uninstall the
Python Environmentextension?zhu181 commentedon Oct 16, 2025
No, this issue will not occur if the
Python Environmentextension is disabled or uninstalled.Edit:
Also, as I said, this issue will not occur if I set
python.useEnvironmentsExtension: false.github-actions commentedon Nov 15, 2025
Hey Karthik Nadig (@karthiknadig) Eleanor Boyd (@eleanorjboyd), this issue might need further attention.
Zhu Liyang (@zhu181), you can help us out by closing this issue if the problem no longer exists, or adding more information.
zhu181 commentedon Nov 16, 2025
The problem can be solved by launching vscode in a terminal that has already activated the pixi environment, which suggests that the problem arises from that the pixi python environment is not properly activated by the python environment extension.
I think that this might no longer be a serious issue. However, I would like to know that if it is possible to let user customize the process that the python environment extension activates the python environment or give more information about the process(the way that it is different from that when not enabling the extension) in docs?
Should I close this issue Eleanor Boyd (@eleanorjboyd) Karthik Nadig (@karthiknadig) ?
eleanorjboyd commentedon Nov 18, 2025
does using this specific extension that integrates with the Python Environments Extension for Pixi help if you enable this and use your pixi environment managed by this one? https://github.com/renan-r-santos/pixi-code/tree/main/src
zhu181 commentedon Nov 20, 2025
It (https://github.com/renan-r-santos/pixi-code/tree/main/src,
renan-r-santos.pixi-code) helps in my case. Thanks very much for the information!eleanorjboyd commentedon Nov 20, 2025
great news!
cc Renan Santos (@renan-r-santos)