Integrating Python and Jupyter Notebook with Visual Studio Code - python

Summary:
I am trying to set up the Jupyter Notebook extension for VSCode but I can’t get my Python code snippets to run. There is no "Run" button to select. I can't select a Python/ Jupyter kernel to work with.
The problem in greater detail:
If you check out this YouTube clip at exactly 1 minute and 46 seconds (here - - you can pause it to see the still) this is what I am trying to accomplish.
But here is what my VSCode actually looks like:
As you can see in my screenshot, the “Play” icon to the left of my code snippet is missing. Along the top border, the other “Play All” button is missing.
In Microsoft’s official VSC doc for setting up Jupyter Notebooks, right near the top under this specific heading: “Create or open a Jupyter Notebook” their interface has way more options at the top of the cells when compared to mine. They have buttons named: “Variables”, “Restart”, “Interrupt”. That is expected for Jupyter Notebook funcionality. Mine is missing all of that. Clearly there is something wrong with my setup.
Other SO users report having ms-python extension installed (which includes Jupyter functionality) in addition to stand-alone Jupyter Notebook extensions installed. Apparently in situations when you have multiple Python extensions installed, they can interfere with each other. So after much wrangling, I resolved to uninstalling all my Jupyter extensions and keeping only the main ms-python extension.
The issue persists.
Here is a complete list of all my VSC installed extensions:
$ code --list-extensions --show-versions
arcticicestudio.nord-visual-studio-code#0.19.0
batisteo.vscode-django#1.8.0
bibhasdn.django-html#1.3.0
Compulim.indent4to2#0.1.2
Equinusocio.vsc-community-material-theme#1.4.4
Equinusocio.vsc-material-theme#33.5.0
equinusocio.vsc-material-theme-icons#2.3.1
esbenp.prettier-vscode#9.8.0
formulahendry.code-runner#0.11.8
GulajavaMinistudio.mayukaithemevsc#3.2.3
icrawl.discord-vscode#5.8.0
janisdd.vscode-edit-csv#0.6.10
lonefy.vscode-JS-CSS-HTML-formatter#0.2.3
m-thorsen.vscode-materia#0.4.4
ms-python.python#2022.12.1
NikolaosGeorgiou.html-fmt-vscode#0.6.0
PKief.material-icon-theme#4.20.0
samuelcolvin.jinjahtml#0.17.0
streetsidesoftware.code-spell-checker#2.7.0
yzhang.markdown-all-in-one#3.5.0
zamerick.black-ocean#1.0.2
zhuangtongfa.material-theme#3.15.2
There is a bit of noise in that list. I'm not sure what all of them refer to. But for certain there is only one Python app (ms-python) and nothing related to Jupyter.
Some SO members elsewhere have suggested selecting / setting the base Python version/environment. When I click the “Select Kernel” button at the top right (as illustrated in the screenshot below), it reveals an empty menu (see at the top left):
Here is “Python v3.8.5 (conda)” showing as selected at the bottom and slightly to the left:
Another exhaustive question and answer thread elsewhere on Stack Overflow involving an issue similar to mine includes many, many creative potential solutions: jupyter server : not started, no kernel in vs code
In that Stack Overflow question, one prominent member answers:
How I solved it:
Press Command+Shift+P to open a new command pallete
Type >Python: Select Intepreter to start jupyter notebook server
Open the notebook again
Someone else suggests:
Press Command+Shift+P on mac, Ctrl+Shift+p on windows
Type Jupyter: Select Interpreter to start Jupyterserver
It would show you a dropdown of python versions installed.
I chose python 3.7.5 and it worked for me. you can choose the python version installed on your machine.
Another member says:
I have seen all possible solutions but not work, finally I just
upgrade jupyter, notebook, and jupyterlab, like pip install -U jupyterlab, and I can choose the kernel in VScode!
None of the above resolves my issue.
Here is a quirky new twist: After installing jupyterlab with pip, after restarting VSC, I encountered this weird error:
It’s a condition box. I clicked: “Yes”.
Jupyter is still not integrated properly.
UPDATED: Based on JialeDu’s answer, I followed his instructions. I created a video shared below. It doesn’t quite capture every step, but I did follow all the steps. What you can see in the gif video is that my current working directory is a folder called Test, a new terminal has been opened, and there is an active virtual environment with $ source .venv/bin/activate. Next, as you can see in the .gif, I am able to select the latest Python interpreter v3.10.5 showing as “Recommended”. So that is progress. I couldn’t do that before. Going the virtual environment route was a great idea. However, I am still not able to get the Jupyter Notebook interface as the end product shared by JialeDu. When I invoke Ctrl + Shift + P again and type > create jup a new Jupyter Notebook file is created but the kernel I selected previously does not show and the expected Jupyter interface buttons and options are still not showing. Here is the video demo in .gif formatting:
Please note: Here you can see a list of 4 available Jupyter extensions:
I avoided installing any of these separate Jupyter extensions in the store because according to the instructor (and as confirmed on the official Microsoft Python extension ms-python page which I already have), it includes Jupyter built in. From the ms-python extension entry, it explains: "This Python extension will automatically install the Pylance and Jupyter extensions to give you the best experience when working with Python files and Jupyter notebooks."
Even though other Stack Overflow users indicated that mixing the above extensions such as ms-python with ms-toolsai was a bad idea, I experimented with having them both installed anyway. Didn’t fix the issue. I tried uninstalling ms-python and keeping ms-toolsai. Still no dice.
By the way, I’ve also got the latest version (as of this writing) of VSCode v1.70.1 installed:

VS Code has prompted you to install the suggested extension. Python and Jupyter.
Also it helps to make sure your vscode is up to date.
The play button of the interface is provided by Jupyter, but if you want to run the code (select the kernel), you must also install the Python extension.
It’s a condition box. I clicked: “Yes”.
You don't have to click Yes because there is a time delay. By the time you click Yes, the environment may not be ready.
If you are using a virtual environment, you can select the virtual environment's interpreter and then create a new terminal to activate the environment.
Complete creation steps:
Prerequisites:
The machine has python installed,
and vscode has Python and Jupyter extensions installed.
steps
Create a new working folder Test
Open the folder Test with vscode
new terminal
Type the command python -m venv .venv to create a virtual environment
After the creation is complete, use the command .venv/scripts/activate to activate the virtual environment
Or Ctrl+Shift+P --> Python:Select Interpreter, select the interpreter of the virtual environment and then create a new terminal to activate the environment
Create new jupyter file using command palette command Create:New Jupyter Notebook
Or right click --> New File --> name ends with .ipynb
The virtual environment you just selected will be enabled as the kernel by default.

I discovered a very simple answer: I installed the VSC package in the official Snap repo.
VSC and Python-Jupyter look to be all playing nice together now:

Related

Jupyter notebooks in VSCode not showing up to date python kernel list

For a couple of months I've had issues with working with virtual envs, python notebooks & jupyter in VSCode, for some reason, a lot of the time, it cannot find kernels I added, even though I can see them when running jupyter kernelspec list. It seems random whether they appear or not. Reloading the screen and restarting vscode does not help, neither does disabling and re-enabling the python and jupyter extensions. I tried both from the button, select kernel and through the command palette. I usually start vscode from the command line, with my virtual environment activated. Moreover, a lot of kernels which I've deleted a long time ago still show up in the list.
Vscode and laptop are both up to date.
I've had issues with the integrated jupyter notebooks before, at first formatting and autocomplete didn't work in the notebook (but they did in regular python files).
I'm on MacOS, if that matters.
Sorry to tell that this is a bug, see bug report:
Can't see or select Jupyter kernel's from Kernel drop-down
And now there's a workaround that:
Find where python is installed
Select the command Python: Select Interpreter
Next select Enter interpreter path
Next enter the path into the input box and hit Enter
This interpretr will now work in VS Code and you might need to re-load
VS Code just once more to get it working in jupyter extension.
Please have a try.

VSCode mouse-hover on Python Keywords not working

While beginning to teach a friend how to code in Python, I observed something in his VSCode that was different than my own - he could mouse-hover on a Python keyword such as as import or def, and it would show the Python Manual definition for the keyword. My own VSCode does not do this.
I have tried everything I can think of to find why this won't work for me, and I'm at a complete loss. I have compared my installation process to my friend's, followed the exact same steps I provided him for setting up his environment, and do not get the same results. I tried completely removing Python, VSCode and all residual VSCode files from my laptop, and reinstalling in the same order he did, still to no avail.
I followed those same steps on a different laptop at work, and I can get the MouseHover to work there. The only feasible difference I can think of is that my work machine is not using the same Windows account as both my home PC and laptop - but I do not have Settings Sync enabled so I can't see why that should matter anyway.
Any thoughts for what I'm overlooking to get this functionality working properly in VSCode for myself? I don't particularly need it, but I'd like to be in the same environment as my friend while I teach him.
We're both on VSCode 1.55.2, Python 3.9.4, using a virtual environment created from VSCode's Powershell terminal, then restarted VSCode to get it to detect the virtual environment and use it as default for Terminals, and finally pip installed and enabled Pylint as the linter.
Editing to add steps followed - I just went through all of this again on my home laptop, after completely removing all traces of VisualStudio2019 and Code, and Python. Note: this was on Windows 10, with all updates applied
Install VSCode
Install VSCode Python extension, MS version
Install Python 3.9.4
Open a new folder in VSCode, then open Terminal
python -m venv venv
create a something.py in the Explorer pane
VSCode should detect the venv and offer to load it. Say yes
VScode may also indiacte that pylint is not installed, Allow it to install, or manually pip install pylint from the venv.
in something.py type out import random
Mouse over import -- this is the discrepancy. On both of my home machines, there is no mousehover anything. on both of my friend's machines, as well as my machine at work, the following is shown:
With help from riov8's comment on the original post, I was able to compare the Experiment Groups that were in effect for each of the environments.
In VSCode, with a Python file loaded, switch to the Output panel (in same area as Terminal) and change the dropdown to Python.
Scroll to the top of this panel, and examine the Experiment Groups you belong to.
Most of the experiment groups do not apply to this issue.
Look specifically for either pythonJediLSP, pythonJediLSPcf or if
you have neither.
If you are experiencing the issue I describe, then at the bottom of this same panel, when you first mouse over a python keyword after starting VSCode, you will get errors similar to the following:
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Providing the line is now done in the functions themselves like `Script(...).complete(line, column)`
sys_path=sys.path,
%UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Providing the column is now done in the functions themselves like `Script(...).complete(line, column)`
sys_path=sys.path,
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Deprecated since version 0.17.0. Use the project API instead, which means Script(project=Project(dir, sys_path=sys_path)) instead.
sys_path=sys.path,
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:626: DeprecationWarning: Deprecated since version 0.16.0. Use Script(...).infer instead.
script.goto_definitions(), request["id"]
The following scenarios resolved the issue for me:
User belongs to experiment group pythonJediLSPcf:
File > Preferences > Settings
in Search Settings, begin typing python.experiment
under Python> Experiments: Opt Out From, click Edit in settings.json
in the section "python.experiments.optOutFrom", add "pythonJediLSPcf" (quoted)
if it does not exist, add another section "python.experiments.optInto"
In the OptInto section, add "pythonJediLSP" (quoted)
Save the settings.json, and restart VSCode.
MouseHover tooltips on Python keywords now should work.
User does not belong to either pythonJediLSPcf or pythonJediLSP:
File > Preferences > Settings
in Search Settings, begin typing python.experiment
under Python › Experiments: Opt Into, click Edit in settings.json
In the "python.experiments.optInto section, add "pythonJediLSP" (quoted)
Save the settings.json, and restart VSCode.
MouseHover tooltips on Python keywords now should work.
User belongs to pythonJediLSP:
It was my experience that this was the experiment required in order for the feature to work properly. If you have this experiment enabled already, then ensure you do not also have pythonJediLSPcf. Disable it if you do, following the instructions in the first sub-heading. If it continues to not work, then there may be another conflict that I did not experience myself.
I am posting a GitHub issue regarding this problem and will update this answer with the link when done.

VSCode Jupyter cannot update kernels automatically

I'm working on Ubuntu by remoteSSH, and I updated python kernel in my vitual environment named nn form 3.7.9 to 3.8.5, however, I still find the old kernel standing in the jupyter kernel list. I want to know how to delete the old kernel name from the kernel list.
I've replaced python 3.7.9 and python3.6.4 with python 3.8.5, but the old kernels didn't disappear, I want to delete them manually.
Moreover, I can't select Python 3.8.5 from the kernel list.
I had the same problem and the following might help someone else encountering the issue:
Reload VS Code Window by Ctrl+Shift+P and selecting Reload Window.
Reload the Python and Jupyter extensions under the Extensions in the Side Bar.
Quit and relaunch VS Code.
It seems that VS Code is not that quick to update the interpreter list.
Here's a workaround: in the command palette, search for Python: Select interpreter. You then get a dropdown with a little refresh button in the top right corner. After you refresh this list the new interpreter will be listed as a kernel for jupyter.
It looks like you have fixed your issue already. But in case anyone else hits this, or a similar issue. VS Code jupyter support looks in the standard jupyter locations for any installed kernelspecs you can see those directories listed here:
https://jupyter.readthedocs.io/en/latest/use/jupyter-directories.html#data-files
VS Code should pick up anything that Jupyter could pick up. So if you happen to have jupyter installed in a python environment then from the commandline you can run jupyter kernelspec list to see a list of all the kernelspecs installed on the system.
Ok I figured it out. There are kernel register files in the directorry
.local/jupyter/kernels/pythonxxxx
I manually delete python379 and python364 then everything is totally right.

Jupyter Book build fails after default create

I'm still learning how this all works, so please bear with me.
I'm running conda 4.8.5 on my Windows 10 machine. I've already installed all necessary Jupyter extensions, I think (Jupyter Lab, Jupyter Notebook, Jupyter Book, Node.js, and their dependencies).
The problem might have to do with the fact that I've installed Miniconda on a separate (D:/) drive.
I've set up a virtual environment (MyEnv) with all the packages I might need for this project. These are the steps I follow:
Launch CMD window
$ conda activate MyEnv
$ jupyter-lab --notebook-dir "Documents/Jupyter Books"
At this point a browser tab opens running Jupyter Lab
From the launcher within Jupyter Lab, open a terminal
$ cd "Documents/Jupyter Books"
$ jb create MyCoolBook
New folder with template book contents gets created in this directory (Yay!)
Without editing anything: $ jb build MyCoolBook
A folder gets added to MyCoolBook called _build, but it doesn't contain much more than a few CSS files.
The terminal throws this error traceback which wasn't very helpful to me. The issue may be obvious to an experienced user.
I am not sure how to proceed. I've reset the entire environment a few times trying to get this to work. What do you suggest? I'm considering submitting a bug report but I want to rule out the very reasonable possibility that I'm being silly.
I asked around in the Github page/forum for Jupyter Book. Turns out it's a matter of text encoding in Windows (I could have avoided this by reading deep into the documentation).
If anyone runs across this issue just know that it can be fixed by reverting to some release, Python 3.7.*, and setting an environment variable (PYTHONUTF8=1) but this is not something I would recommend because some other packages might require the default system encoding. Instead, follow the instructions in this section of the documentation.

VS Code: Can't load anaconda environment on Python Interactive

I am using VS Code in Windows 10.
I am able to use the dependencies in my conda environment if I Run the Python Script in Terminal. However, I am not able to use the environment in the Python Interactive shell.
For instance, I am using my datascienceenvironment which has the package fuzzywuzzy. I have included the env path into the settings.jsonas the python.pythonPath. But the Python version used in the Jupyter Notebook is the default anaconda base C:\\ProgramData\\Anaconda3\\python.exe
What is also extrange, is that if I run !conda list within the Notebook, I can see the fuzzywuzzy package.
What am I missing?
Thanks!
I'm a developer on the Interactive Windows. Priyatham has already pointed you in the right direction. Currently our Window tries to use the currently selected environment (the one in the lower left corner). But we didn't want to totally block customers if they didn't have Jupyter installed in each environment. So if the selected interpreter doesn't have Jupyter in the environment we'll check other environments on the system, looking for the closest python version match to the currently selected one. If we find something with Jupyter we'll launch the Interactive Window using that. In this case we do show a popup in the lower right indicating that we didn't find Jupyter and that we are falling back on a different environment, but this message can be pretty easy to miss.

Categories

Resources