I want to use Jupyter Notebooks inside VS Code running within WSL 2. Regular Python works within WSL without a problem, that means the base conda enviroment is connected and I can run .py files. Moreover, I’m able to run the command jupyter notebook which will start the notebook server within WSL exposing the localhost to the Windows system.
However, I want to test the new Jupyter Notebook feature within VS Code, which is not working for me; VS Code shows: Jupyter Server: No Kernel and Python: Not Started (see screenshot below).
My testing machine is:
WSL 2 (using Pengwin distribution) on Windows 20H1 (19041.21)
VS Code 1.41 (having the Python extension installed)
Anaconda
What I did:
Open WSL
Open VS Code in my target directory (code .)
Create a new Notebook file from the command palette Python: Create New Blank Jupyter Notebook
Create dummy print("test")
Run Cell will trigger the warning:
Is there anything I’m missing?
The VS Code insiders actually works out of box with Jupyter notebooks. Just remember to run code-insiders . instead of code ..
Another solution would be, according to this website, start the VS Code in windows, then use WSL 2 as a remote server.
The code installation on wsl2 needs a restart to work properly, I was told by a notification, and in the question's comments you may see people experiencing the same issue(' it's working now. not sure why'). So before looking upon any elaborate solution, just restart code.
Related
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:
In vscode, While running my python script on interactive python window in other conda environment (apart from base environment), I get the following error -
'Timed out waiting to get a heartbeat from kernel process.'
Script works fine in base environment but not in other envrionments. Also, this is specific for python interactive window. It works fine in terminal.
Has anyone faced this issue as well?
According to your description, code works fine in base environment and terminal, while got error in other conda and interactive window. So try to solve the problem with following steps:
Create a new conda environment, reference: python-environments
To use the window with a .py file, select the Run Current File in Python Interactive window command from the Command Palette. More information and details you can reference: python-interactive.
Here you'll get the output. The following is a demo screenshot.
Considering your question is related to python extension, if the above not work, reinstall python extension and try again.
I'm working with VSCode on an Ubuntu 18.04 machine. Everything is fine except that I can't get back to the previous command in Python Interactive Window via Arrow Up. It works in the integrated terminal though, integrated shell is /bin/bash.
I have no idea where this is coming from. I changed "keyboard.dispatch" to "keyCode", but that's not the problem. I also tried different versions of the python-extension.
Do you have any idea?
Thanks!
Not sure if this is helpful for you as an Ubuntu user, but I came across the same problem in Windows this week. In my case, the issue cropped up after I installed the Python extension in VS Code (or at least I didn't notice the issue before this). The combination of using the Python extension, Git Bash for terminal, and python virtual environment killed the up arrow feature for me. Using python outside a virtual environment works fine. My solution was to use Command Prompt as the terminal when I wanted to use a virtual environment interactively.
In my situation, although I delete all the shortcuts of UpArrow, I still can get the previous command through UpArrow in Python interactive. This means there's no way to configure this shortcut, it was built in the plugin which built in Python extension.
So it's some problem with your Python extension, but you said you have tried to install a different version of Python extension but still not work. Make sure you have deleted it completely -> delete the extension folder manually(it's under C:\Users[UserName].vscode\extensions\ms-python.python-xxx).
For my current job it would be extremely helpful to be able to configure a virtualenv with the appropiate libraries versions, and be able to run either a python project, or cells in jupyter. This is because some people at my job work with jupyter, and some with Python, and sometimes both, and this way I would have a centralized program that could run both types, which I have not found outsaid of the paid version of PyCharm, which my company does not provide.
I just learned a few days ago about Windows Subsystem for Linux, WSL, and that it can be launched from withing Visual Studio Code, so I feel like this is my best bet to achieve that dual nature of programming from just ONE program, instead of running several like in the past.
As of right now, I have a repository cloned with WSL for a git project with different ".py" files, I open it with VSC, then open the terminal inside VSC, and I can both edit the Python code, and run it on the terminal, using bash commands as I would if I were in Ubuntu (I am doing all this from windows but can switch to Ubuntu if it would mean to be able to do this type of setup).
When I run with "Run Python file in terminal", it uses the virtualenv I have previously created.
The problem is, with Jupyter, it does not detect I have the libraries installed (like Pandas for example)
Description of my process with Jupyter so far: With the WSL console, I launch a jupyter notebook &. I then connect to that server, usin the VSC option for "Specify Local or Remote Jupyter server for connections", use the "Existing" option, copy the URL, then I go to the ".ipynb" file and start running code.
If, in a Jupyter cell, I do
import os
os.environ['VIRTUAL_ENV']
I can see my virtual enviroment. If right after that, I run import pandas I get ModuleNotFoundError: No module named 'pandas'
If I do !pip freeze I can see all the libraries and right versions that I have installed in that enviroment.
I feel like I am almost there but something is missing. My guess is that import might be going to some default installed Python, and not the one from the enviroment, for some reason I am missing.
Solved by doing what this answer suggests. The rest of answers may be of help, too:
https://stackoverflow.com/a/51036073/6028947
Here is my setting
and this is my script
I am trying to use jupyter notebook in pyCharm, but it kept using python2 instead of python3.
Any idea about this problem?
Add:
this pic is running jupyter notebook in chrome.
My problem was that I had multiple kernels, and PyCharm launches the default kernel. One approach might be to configure PyCharm to specify the kernel of choice to start up, I didn't investigate how to do that. I simply changed the default kernel in Jupyter and this worked for me (I have a virtualenv for tensorflow). c.MultiKernelManager.default_kernel_name = 'tensorflow'.
The preferences image you show is indeed how you would setup your interpreter for PyCharm, but that's not what the output/logging of PyCharm looks like. I'm guessing that's a jupyter-notebook display, which means you are running into the issue in jupyter-notebook and not PyCharm. So you need to change your setup for jupyter. Based on some quick searching pip install jupyter will install a python 2.7 version of jupyter. Sounds like what you want is
pip3 install jupyter
which will install the python3 version for you. You will likely have to uninstall your current version of jupyter.
When you kick off Jupyter-notebook from within PyCharm there is a configuration which is created. If the configuration is initially 2.7 ( I think it defaults to the current interpreter), and then keep using that same configuration, it wouldn't matter the state of the current project interpreter because it would be using the value saved in the run configuration.
You can modify your run configuration by
Run | Run...
Edit Configurations...
Select your Jupyter Notebook run configuration on the left (here is untitled4)
Make sure the python interpreter is correct here on the right
I was able to start a jupyter notebook like this and get it to output python 3 by doing this. Hope this is what you are needing.