Open ipynb on Visual Studio Code, display json - visual-studio-code

When I open an ipynb file, I just see a json file. I tested with all files of :
https://github.com/MicrosoftLearning/mslearn-ai900.git
I have on my machine :
Python 3.9.6 installed
Installed with pip command : jupyterlab, notebook and voila
On Visual Studio Code (version 1.59.0) :
Python 3.9.6 64-bit is the current interpreter.
I can create a new notebook : "CTRL+SHIFT+P --> Jupyter : Create new blank notebook"
and I saw on the down-right "Jupyter Server:local". I can execute code : print("Hello, Python!"), but when I try to save file, I have this error :
Failed to register 'Untitled-1' : File name file:///*ThePathToSave*/Test.ipynb is not supported by Jupyter.
Extensions installed :
Jupyter v2021.8.1195043623
Pylance v2021.8.1
Python v2021.8.1105858891
List of kernel : (command) jupyter kernelspec list
Available kernels:
.net-csharp C:\Users\antho\AppData\Roaming\jupyter\kernels\.net-csharp
.net-fsharp C:\Users\antho\AppData\Roaming\jupyter\kernels\.net-fsharp
.net-powershell C:\Users\antho\AppData\Roaming\jupyter\kernels\.net-powershell
python3 c:\python\python39\share\jupyter\kernels\python3
Thanks for your help.

I re-install all Visual Studio Code.
I followed this answer to uninstall 'completely' VS Code :
https://debug.to/1074/how-to-uninstall-vs-code-completely
and after, I re-install Visual Studio Code.
Now I can open all files.
Thanks to #IanHuff

Related

Why Does the VS Code Jupyter Extension Keep Timing-out Trying to Find a Kernel That Exists?

I need to set up virtual environments for each language that I use. To do this, I'm running the Ubuntu 20.04 LTS Windows Subsystem for Linux (WSL) on Windows 10. Within WSL, I'm using Anaconda, installed in /usr/local/Anaconda, to create conda virtual environments for each language (i.e. one environment contains all my Python stuff, another contains my R stuff, etc.).
Since WSL doesn't come with a GUI, I'm using Visual Studio Code's (VSCode) Jupyter Notebook Extension to run Jupyter Notebooks to see plots/graphics. So far, I managed to easily create conda environments for Python (with ipython and ipykernel) and R (with IRkernel) and run their code in a notebook via the extension. Each time I set up an environment, the extension is easily able to find the kernel, connect to it and run the code.
However, I've not been able to set up an environment for Julia. I followed the documentation on the Julia website for installing the kernel, which is successfully found by the extension. But, when I try running a cell, the extension says it is trying to connect to the kernel, only for it to timeout and fail.
Here are the steps I have taken so far:
Create a clean conda environment (conda create -n Julia && conda activate Julia)
Install the latest version of Julia (conda install -c conda-forge julia)
Install the latest version of Jupyter (conda install -c conda-forge jupyter)
Install the Julia kernel with the built-in Julia package manager (using Pkg; Pkg.add("IJulia"))
Build the IJulia package (using Pkg; Pkg.build("IJulia"))
Confirm the presence of the Julia kernel (jupyter kernelspec list) which indeed shows the presence of a Julia kernel
Reload the VSCode connection to WSL (Ctrl + Shift + P; >Reload Window)
Shut down WSL via CMD (wsl --shutdown) for changes to take effect and reconnect
After I restart VSCode and WSL, the extension shows an option to use the Julia kernel installed in my conda environment: Julia 1.7.2 (~/.conda/envs/Julia/bin/julia). But when I create a cell and run code in a notebook, the extension creates a popup saying that it is connecting to the kernel and after some time an error message shows up:
Failed to start the Kernel.
Unable to start Kernel `Julia 1.7.2` due to connection timeout.
View Jupyter log for further details
I can also see the kernel spec JSON file in ~/.local/share/jupyter/kernels/julia-1.7/kernel.json
{
"display_name": "Julia 1.7.2",
"argv": [
"/home/USER/.conda/envs/Julia/bin/julia",
"-i",
"--color=yes",
"--project=#.",
"/home/USER/.conda/envs/Julia/share/julia/packages/IJulia/AQu2H/src/kernel.jl",
"{connection_file}"
],
"language": "julia",
"env": {},
"interrupt_mode": "signal"
}
The log file starts showing problems here:
info 17:50:48.378: Process Execution: cwd: ~
cwd: ~
warn 17:50:48.893: StdErr from Kernel Process [91m[1mERROR: [22m[39m
warn 17:50:49.138: StdErr from Kernel Process LoadError:
warn 17:50:49.795: StdErr from Kernel Process ArgumentError: Package IJulia not found in current path:
- Run `import Pkg; Pkg.add("IJulia")` to install the IJulia package.
The extension says it cannot find the IJulia kernel. This perplexes me because I can see the kernel spec in my home directory, the jupyter binary I installed from conda says that its there and the Jupyter Notebook extension can see the kernel. I have no explanation as to why the extension can see the kernel, match up the kernelspec but not be able to connect to it. Help would greatly be appreciated!

Remote-SSH resulted in an error command opensshremote.openEmptyWindow not found

Remote-SSH resulted in an error command opensshremote.openEmptyWindow not found.
I am on Arch Linux, vscode-oss from community repository package code version 1.64.
How to fix?
> pacman -Ss vscode
community/code 1.64.0-1 [installed]
The Open Source build of Visual Studio Code (vscode) editor

Visual Studio Code does not render ipywidgets correctly

I am having issue using Visual Studio Code. the code seem to have issue with ipywidgets.
the code is below (very simple interactive chart)
from ipywidgets import interact
import ipywidgets as widgets
import numpy as np
import plotly.graph_objects as go
#interact
def foo(beta=(-10,10,1)):
myx = np.arange(0,100,1)
myy = myx *beta
print ('debug:',myy)
fig = go.FigureWidget()
fig.add_scatter(x= myx, y=myy)
fig.show()
the same code works just as expected in Jupyter Notebook.
I dig further and it seems:
"widgets require us to download supporting files from a 3rd party website. Error loading plotlywidget: ^4.14.3
https://github.com/microsoft/vscode-jupyter/wiki/IPyWidget-Support-in-VS-Code-Python
This is where I got stuck.. What's the next then to fix this issue?
any guidance much appreciated
thanks
Some of the issues I've seen reported seem to have started after the release of ipywidgets 8.
I don't understand exactly what's broken (or if it's broken), but downgrading to version 7.7.1 fixed all issues I was having.
If you want to try it as well, you can run: pip install -U ipywidgets==7.7.1
There two things you can explore.
Check modules:
Run the jupyter --version command inside your conda or virtual environment.
You should see something like this:
IPython : 8.4.0
ipykernel : 6.9.1
ipywidgets : 7.7.1
jupyter_client : 7.2.2
jupyter_core : 4.10.0
jupyter_server : 1.21.0
jupyterlab : 3.4.8
nbclient : 0.6.4
nbconvert : 6.5.0
nbformat : 5.4.0
notebook : 6.4.12
qtconsole : not installed
traitlets : 5.4.0
If there are any of those modules not installed (Apart from qtconsole) try installing them (with pip install "module") and restarting the kernel.
Check Jupyter nbextension.
As mentioned here after running pip install ipywidgets try:
jupyter nbextension enable --py widgetsnbextension
Note: If you are using virtual environments (including conda environments) the recommended way to activate the extension is to run
jupyter nbextension enable --py --sys-prefix widgetsnbextension
I hope this helps.

VSCode settings for search paths with PEP582 & ipykernel

I used python package managers (PyFlow/PDM) which supports PEP582: packages are installed in the local directory {workspace_path}/__pypackages__/.
I added some configs in settings.json to let VSCode know how to find the packages:
{
...,
"python.autoComplete.extraPaths": [
"__pypackages__/3.7lib"
],
"python.analysis.extraPaths": [
"__pypackages__/3.7/lib"
]
}
I wrote some code, and the completion and inline code docs works well with above settings.
# main.py
#%% jupyter cell
import sys
print(sys.path)
import pandas as pd
print(pd.__version__)
#%%
print("hello world")
I ran my main.py in both ways:
Run the file with VSCode (ctrl + opt + N)
Run the jupyter cell with ipykernel (shift + enter). VSCode asked me to install ipykernel at the first time, but it installed with pip, and not installed into __pypackages__.
but it failed and showed: ModuleNotFoundError: No module named 'pandas'.
I check the search path with sys.path, it doesn't contain __pypackages__ for python modules:
['{$HOME}/{workspace}',
'{$HOME}/.vscode/extensions/ms-toolsai.jupyter-2021.5.745244803/pythonFiles',
'{$HOME}/.vscode/extensions/ms-toolsai.jupyter-2021.5.745244803/pythonFiles/lib/python',
'{$HOME}/.pyenv/versions/3.7.10/lib/python37.zip',
'{$HOME}/.pyenv/versions/3.7.10/lib/python3.7',
'{$HOME}/.pyenv/versions/3.7.10/lib/python3.7/lib-dynload',
'',
'{$HOME}/{workspace}/.venv/lib/python3.7/site-packages',
'{$HOME}/{workspace}/.venv/lib/python3.7/site-packages/IPython/extensions',
'{$HOME}/.ipython']
My questions:
How to configure settings.json to add search paths (__pypackages__/3.7lib) for Python modules.
How to configure settings.json to let VSCode Jupyter server launch IPython with ipykernel installed in __pypackages__/3.7lib
"python.autoComplete.extraPaths": [
"${workspaceFolder}/__pypackages__/<major.minor>/lib"
],
"python.analysis.extraPaths": [
"${workspaceFolder}/__pypackages__/<major.minor>/lib"
],
Try this in settings.json
Replace <major.minor> with your python version like 3.10, 3.9, etc
I found two ways to solve the sys.path problem.
The easiest one would be to launch VSCode with the following command:
pdm run -p <path-to-profile> code <path-to-folder-or-workspace-file>
This ensures that PYTHONPATH contains __packages__\... directories for VSCode to use.

Conda virtual environment for IPython

I'm fairly new to the Python scene. My problem is that when I launch a jupyter notebook from an Anaconda Powershell with my DataScience virtual environment activated, the notebook does not have my virtual environment in it's PATH, and therefore cannot find some packages (like plotly and progress). The same is true when I launch VS Code from Anaconda Navigator with DataScience activated. When I run import plotly in an interactive window, I get ModuleNotFoundError: No module named ‘plotly’. But when I run this line in the terminal within VS Code, it runs without error.
So I have run the following commands in various shell/terminal sessions:
import sys
print(sys.path)
In a VS Code terminal I get:
['', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience\\python37.zip', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience\\DLLs', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience\\lib', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience', 'C:\\Users\\adiad\\Anaconda3\\envs\\DataScience\\lib\\site-packages']
In an interactive window in VS Code I get:
['C:\\Users\\adiad\\AppData\\Local\\Temp\\04e2b30c-4fc3-4aa9-9567-3aba17081a73', 'C:\\Users\\adiad\\Anaconda3\\python37.zip', 'C:\\Users\\adiad\\Anaconda3\\DLLs', 'C:\\Users\\adiad\\Anaconda3\\lib', 'C:\\Users\\adiad\\Anaconda3', '', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\adiad\\.ipython']
In a jupyter notebook running in my browser I get:
['C:\\Users\\adiad\\Anaconda3\\envs\\test', 'C:\\Users\\adiad\\Anaconda3\\python37.zip', 'C:\\Users\\adiad\\Anaconda3\\DLLs', 'C:\\Users\\adiad\\Anaconda3\\lib', 'C:\\Users\\adiad\\Anaconda3', '', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Users\\adiad\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\adiad\\.ipython']
The IPython session don't appear to reference my virtual environment. So my question is: what do I need to do make IPython run with same environment as my terminal?
I found the following SO question which seems to answer my question, but I find it hard to believe that everyone is following this practice.
How to start an ipython shell(not notebook) within a conda or virtualenv
Here's my configuration:
conda version : 4.7.12
conda-build version : 3.18.8
python version : 3.7.3.final.0
virtual packages :
base environment : C:\Users\adiad\Anaconda3 (writable)
channel URLs : https://conda.anaconda.org/conda-forge/win-64
https://conda.anaconda.org/conda-forge/noarch
https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
package cache : C:\Users\adiad\Anaconda3\pkgs
C:\Users\adiad\.conda\pkgs
C:\Users\adiad\AppData\Local\conda\conda\pkgs
envs directories : C:\Users\adiad\Anaconda3\envs
C:\Users\adiad\.conda\envs
C:\Users\adiad\AppData\Local\conda\conda\envs
platform : win-64
user-agent : conda/4.7.12 requests/2.22.0 CPython/3.7.3 Windows/10 Windows/10.0.18362
After doing further digging, my problem ought to be filed under, "knowing enough to be dangerous." My problem was ultimately caused by the fact that the jupyter package hadn't yet been installed in my new environment. So whenever I attempted to launch an IPython session of some kind, either in VS Code or in a browser, the application would look in my environment and see that the IPython packages weren't installed. It would then look to other conda environments and use the "nearest" equivalent, which was the base environment. Hence, most of the packages would load, but not all.
The fix to my problem was:
conda install jupyter
Another simple fix:
Launch CMD.exe prompt on Anaconda Navigator
Install: conda install jupyter
And
Conda install plotly