Debug jupyter notebook that uses multiprocessing in Visual Studio Code - visual-studio-code

I use multiprocessing in a juypter notebook to cut down the processing time. Unfortunately it seems it's not possible to use the debugger in Visual Studio Code to debug anything after the Pool has been created. At first I get this error message:
Couldn't find a debug adapter descriptor for debug type 'Python Kernel Debug Adapter'
(extension might have failed to activate)
Followed by this error message for every child process that was created:
Failed to launch debugger for child process X
This is the code in the jupyter cell
from multiprocessing import Pool
from func import x
with Pool(4) as p:
results = p.map(x, [1,2,3,4])
for i in results: # breakpoint
print(i)
It also requires a file 'func.py':
def x(y):
return y
I also tried adding this as suggested in the vscode documentation, but with no effect:
import debugpy
debugpy.debug_this_thread()
Is there any way to debug a jupyter notebook that uses multiprocessing with vscode?
OS: Windows 10
Python: 3.11
VSCode: 1.74.1

Related

Python program in VS Code is not running and I keep getting this error in output

LSP Notebooks experiment is disabled -- Jupyter disabled or not installed LSP Notebooks interactive window support is disabled -- not in LSP Notebooks experiment Python interpreter path: \~\\AppData\\Local\\Programs\\Python\\Python310\\python.exe Starting Pylance language server. \[ERROR 2022-11-17 19:46:13.280\]: sendStartupTelemetry() failed. \[TypeError: Cannot read properties of undefined (reading 'uri') at m (c:\\Users\\DigitechPC.vscode\\extensions\\ms-python.python-2022.20.1\\out\\client\\extension.js:2:665529) at t.sendStartupTelemetry (c:\\Users\\DigitechPC.vscode\\extensions\\ms-python.python-2022.20.1\\out\\client\\extension.js:2:666516)\]
was expecting output for my program but don't know what to do with this

How can I fix the attribute error in VS code when the same code runs perfectly on colab?

I was running the PySINDy package and I kept on getting the module not found-error when I ran this:
pip install pysindy
import pysindy as ps
I fixed this by downgrading the Python in my VS code to 3.8.9. But I do get the following note:
note: This error originates from a subprocess, and is likely not a problem with pip.Note: you may need to restart the kernel to use updated packages.
I am now getting the attribute error when I run the following:
differentiation_method = ps.FiniteDifference(order=2)
Here's the error:
AttributeError: module 'pysindy' has no attribute 'differentiation'
Can someone please help me with this? (I successfully ran the entire code earlier on google colab and the online Jupyter, but I am unable to do it locally. I use MAC os and Jupyter via VS Code.)

Jupyter extension for VSCode on Linux throws error when doing anything Jupyter related

After installing code-oss on Manjaro Linux along with the Jupyter extension for code and jupyter-notebooks itself, the extension seems to error when I try to do anything Jupyter related inside code. If I try to create a new notebook from the command pallete, it pops up a dialog saying this:
Command 'Jupyter: Create New Jupyter Notebook' resulted in an error (command 'jupyter.createnewnotebook' not found)
Code then tells me that the extension activation failed and to open the chrome developer console for more information. This is what the chrome console then says:
extension activation failed Error: Extension 'ms-toolsai.jupyter' CANNOT use API proposal: notebookEditor. Its package.json#enabledApiProposals-property declares: but NOT notebookEditor. The missing proposal MUST be added and you must start in extension development mode or use the following command line switch: --enable-proposed-api ms-toolsai.jupyter
Initial Pop-up error
Chrome Console error
You can solve that problem by inserting"enable-proposed-api": ["ms-toolsai.jupyter"] in the start settings of vs code. So press CTRL+ALT+P -> "Preference: Configure Runtime Arguments" then insert the upper line. After that restart vscode.
Had the same problem and found out this is happening because of the open source version of code - OSS. It does not have the proper config in the product.json.
The accepted solution to add to Runtime Arguments unfortunately didn't work on Arch.
Easiest solution for me was to install code-features from AUR.
You can find more info on arch wiki site
Or directly on microsoft docs regarding oss code issues.
The solution to add "enable-proposed-api": ["ms-toolsai.jupyter"] to the runtime of vscode didn't work for me on Arch linux. But when I started vscode from the command line using the argument "--enable-proposed-api ms-toolsai.jupyter" the problem was solved. So I created a custom desktop launcher for this:
Fist, copy the desktop launcher to your applications folder
$ cp /usr/share/applications/code-oss.desktop ~/.local/share/applications/.
Then edit the new file:
$ nano ~/.local/share/applications/code-oss.desktop
Now add "--enable-proposed-api ms-toolsai.jupyter" to both of the Exec lines like so:
[Desktop Entry]
Name=Code - OSS
Comment=Code Editing. Redefined.
GenericName=Text Editor
Exec=/usr/bin/code-oss --enable-proposed-api ms-toolsai.jupyter --unity-launch %F
Icon=com.visualstudio.code.oss
Type=Application
StartupNotify=false
StartupWMClass=Code
Categories=TextEditor;Development;IDE;
MimeType=text/plain;application/x-code-oss-workspace;
Actions=new-empty-window;
Keywords=vscode;
[Desktop Action new-empty-window]
Name=New Empty Window
Exec=/usr/bin/code-oss --enable-proposed-api ms-toolsai.jupyter --new-window %F
Icon=com.visualstudio.code.oss
I restarted my computer and the problem was solved.

Can a Bokeh server be run in the Jupyter extension of VS Code?

I have VS Code installed together with the Jupyter extension. I have Jupyter and Bokeh locally installed on my computer too.
VS Code:
Version: 1.51.1
Commit: e5a624b788d92b8d34d1392e4c4d9789406efe8f
Date: 2020-11-10T23:31:29.624Z
Electron: 9.3.3
Chrome: 83.0.4103.122
Node.js: 12.14.1
V8: 8.3.110.13-electron.0
OS: Linux x64 5.4.0-56-generic
ms-toolsai.jupyter: v2020.12.411183155
jupyter core : 4.6.3
jupyter-notebook : 6.0.3
qtconsole : 4.7.5
ipython : 7.16.1
ipykernel : 5.3.4
jupyter client : 6.1.6
jupyter lab : not installed
nbconvert : 5.6.1
ipywidgets : 7.5.1
nbformat : 5.0.7
traitlets : 4.3.3
bokeh: 2.1.1
I am trying to run in VS Code a notebook that launches a Bokeh server in order to create then an interactive web application, as in the example below:
import numpy as np
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
output_notebook()
def plot(doc):
x = np.linspace(0,2*np.pi,20)
y = np.sin(x)
p = figure()
h = p.line(x, y)
doc.add_root(p)
show(plot)
When I start Jupyter from a terminal and runs that notebook in a web-browser, everything works fine.
When I use the Jupyter extension in VS Code and display a simple Bokeh figure (using bokeh.io.show(bokeh.plotting.figure)), everything works fine too.
This is when I use the Jupyter extension in VS Code and try to work with a Bokeh server (using bokeh.io.show(callable) as in the example above) that things fail, and that I get the following message in the Webview developer console:
VM79:809 Refused to connect to
'ws://localhost:33727/ws?platform=electron' because it violates the
following Content Security Policy directive: "default-src
'unsafe-inline' 'unsafe-eval' data: https: http: blob:
vscode-webview-resource:". Note that 'connect-src' was not explicitly
set, so 'default-src' is used as a fallback. VM79:809 [bokeh] Failed
to connect to Bokeh server: Could not open websocket VM79:808 [bokeh]
Failed to load Bokeh session
vAIAgpYICYGtqkteUPnD9UbYsp0oXOzsb9lZbLUVOFk4: Error: Could not open
websocket
I tried to uncheck the "Use Default Config for Jupyter" option in the Jupyter extension settings, and specify the following content security policy in the ~/.jupyter/jupyter_notebook_config.py file:
c.NotebookApp.tornado_settings = {
'headers': {
'Content-Security-Policy': "default-src 'unsafe-inline' 'unsafe-eval' data: http: https: ws: wss: blob: vscode-webview-resource:; connect-src http: https: ws: wss:"
}
}
but that surprisingly had no influence (while modifying these settings and running Jupyter in the web-browser had the influence as expected).
I also tried to launch a local Jupyter server from a terminal and make the VS Code Jupyter extension connect to that server (instead of letting VS Code start the Jupyter server by itself), but this surprisingly had no influence either.
Has anybody any ideas what can go wrong ? Has anybody already achieved what I try to do ?
I've already spent a few days attempting to fix the issue and I am now clueless.
Thanks
There is a recent issue on GitHub about this. It is not yet clear whether integration with VSCode is possible, or not. Bokeh expects to be able to make an HTTP connection that it upgrades to a websocket connection, and also to be able to to execute JS code, and it seems as though VSCode may interfere with those requirements. Please feel free to add this information to the linked issue above, since it seems like a novel failure path.

Getting ModuleNotFoundError in python, package exists in virtual environment though

I am new to python and writing my first python file using Visual Studio Code. Till yesterday the code was working fine. But today it is starting to give ModuleNotFoundError. I double-checked the package and the package is there under lib folder of the virtual environment. Moreover, IntelliSense is also working fine, whenever I write import it shows the requests module in the available list. I am facing problems with IntelliSense also, as sometimes it stops working for no reason.
The major change that I did before executing the code is the integration of GitLab with my project.
Python Version: 3.6.2
Operating System: Windows
Git Version: 2.23.0
Pylint Version: 2.3.1
pip Version: 19.2.2
Requests Version: 2.22.0
Pytest Version: 5.1.1
Enabled Extensions:
Npm
Npm Intellisense
Ayu
Code Runner
AYU
Markdownlint
Predawn Themekit
Python by Microsoft
Can anyone have gone through with the same problem? Please help me to resolve this issue.
Attachments
Issue resolved I was running it using Ctrl+Alt+N (code-runner shortcut to run a project)which was creating this issue. But running the file in terminal using Run Python File in Terminal (option in the context menu when we do right-click in source .py file) giving me the expected result without any error.
Then I googled for how to alter this behavior so that I can get the same results by using Ctrl+Alt+N as well.
I found that; I need to add the following script under user's settings.json.
"code-runner.executorMap": {
"python": "h:\\practice\\virtualenv\\Scripts\\activate.bat && python -u"
}
Here is my complete user's settings.json
{
"workbench.colorTheme": "Predawn",
"workbench.iconTheme": "ayu",
"window.zoomLevel": 0,
"code-runner.executorMap": {
"python": "h:\\practice\\virtualenv\\Scripts\\activate.bat && python -u"
}
}