"Running cells requires Jupyter notebooks to be installed" error in VS Code - visual-studio-code

I'm trying to use the new Jupyter integration for the Python extension in VS Code, and I'm getting the above error even though I have Jupyter installed and it works fine from the command prompt.
Here's my environment:
Python extension version 2018.10.1, and I see Run Cell/Run All Cells tooltips above #%% comments.
I've used the Python: Select Interpreter command to select my Anaconda environment, which is at ~/AppData/Local/Continuum/anaconda3/python.exe.
I have Jupyter installed in that interpreter (jupyter.exe is in the Scripts sub-folder under that location), and it runs fine with the jupyter notebook command at the Anaconda prompt.
But whenever I click on Run Cell or press shift-enter, I get this error message:
"Running cells requires Jupyter notebooks to be installed." Source: Python (Extension)
Is there something else I need to do to configure this?

You may give one try by restarting VS Code in following mentioned way [ It worked for me. ]
Open Bash or any other cmd
Activate any conda environment [ See below command ]
source activate base [ means activate base environment ]
Run VS Code instance [ See below command ]
code .
Now when you'll click on Run Cell or press shift-enter, it should work.

The problem is an issue in the VS Code python extension itself. There are a number of issues related to this open in the repository: #3354, #3343, #3330, and the issues are being worked on, see #3374.
The reason, as far as I understand, is that in this case - and in some other cases - the anaconda environment is not activated before running the command. Situations where the environment is activated are e.g. opening a python terminal or running a file in the python terminal, but this also needs to happen for Jupyter, Tests, and so on.
While theoretically, adding the Scripts folder to your PATH, as David mentioned, could help, it did not help in my case. This may just not be enough to properly reflect what happens on activation.
My guess is that we will have to wait for this issue to be resolved in the repo, but if someone else finds a workaround, I'd be happy.

Simply running vscode from the activated environment did not work for me, here is what did:
In terminal (bash) I ran:
conda activate <environment-name>
conda install jupyter notebook
When the install finishes, open vscode from terminal (the same shell with activated environment) with the command:
code .
Notes:
Replace '.' with the path to the directory you want to open if it's not the current directory.
I've written 'conda install ...' but mamba also works.
If the terminal command for 'code ' does not work, it's likely you need to add it to environemnt variables; in such a case, this post might help.

Related

Stale environment variables in Jupyter Notebooks running in vscode using a conda environment kernel

I'm using jupyter inside a conda environment in vscode on Ubuntu and all works great. However, recently I've experienced issues with AWS access tokens expiring inside my jupyter notebook.
It seems that these access tokens get cached at some level, either in the code environment or somewhere in the jupyter layer, but I couldn't figure out where and how to clean it up.
Example:
I'm using aws-vault to generate SSO credentails, that can be accessed in the environment.
After starting a session, I can access the AWS_SESSION_EXPIRATION from my terminal:
echo $AWS_SESSION_EXPIRATION
2022-04-20T17:40:36Z
when I start vscode, open a terminal, I get the same.
When I activate my environment, and run
python -c "import os;print(os.environ['AWS_SESSION_EXPIRATION'])"
I get the same.
However, when I open a new jupyter notebook and select the same environment, and run
import os
print(os.environ['AWS_SESSION_EXPIRATION'])
I get
2022-04-06T11:09:03Z
I've tried locating any files that could cache the env variables either in the conda env or jupyter extension. I've also tried restarting vscode and the machine itself - all without success.
Can anyone help me clean this up?
Thanks!!
Edit:
Another example:
If I set in my terminal
export FOO=BAR
then I can access $FOO as expected through the terminal and jupy notebook within vscode.
Then I close vscode, run
unset FOO
reboot the machine, start vscode new and open a new notebook with the same conda environment.
Within, running
import os
print(os.environ['FOO'])
STILL returns BAR
There seems to be an issue in the vscode-jupyter extension, that somehow stores the environment variables in the kernel.json file.
At the moment of writing this, the issue is still open. Working in ubuntu, this workaround from the issue's thread works for me:
Close code completely
find ~/.vscode/extensions/ms-toolsai.jupyter*
code .
Run a notebook and it will pick up the changes.
With closed vscode, issue the following command to delete cache:
find ~/.vscode/extensions/ms-toolsai.jupyter* -name kernel.json -delete
Source: https://github.com/microsoft/vscode-jupyter/issues/9774#issuecomment-1110328329

PATH variable in Visual Studio Code different from Terminal on Mac

The PATH variable in the integrated Terminal in Visual Studio Code is different from the one in the Terminal app. How can I change it?
I'm using the Intel Distribution for Python from Intel oneAPI and I have both the setvars.sh script called and the conda initialize code in my .zshrc. In Mac Terminal I can properly activate conda environments. However, in the integrated Terminal in VSC, the /usr/bin folder is listed before the conda folders, so the system Python interpreted is called.
EDIT: I initially thought that the Intel Distribution for Python had something to do with it, but the same issue occurs with a regular Anaconda distribution.
It looks like that the problem is that Visual Studio Code inherits the PATH from Terminal but somehow it rearranges the order of the folder, and this confuses the Anaconda activation script. Since I couldn't find a solution, I implemented a workaround by adding the following lines to my .zshrc, right after the Anaconda initialization script:
# Workaround for Visual Studio Code integrated terminal
if [[ -v VSCODE_GIT_IPC_HANDLE ]]; then
conda deactivate
conda activate
fi
I am not 100% sure the right behaviour as I don't use a Mac but you might try launching VS Code from the terminal which has the right environment.
Make sure that code is in your PATH with https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line
Then ensure that all the VScode sessions are closed on your machine and launch it via code from the terminal.

Wrong Python interpreter being used by VS Code

I am on Ubuntu 20.04 and have both Python2 and Python3 installed natively. I have also installed Python through miniforge, a variant of miniconda. In VSCode I have both the MS Python extension and Pylance installed.
I use the miniforge python for my coding. This works perfectly fine in PyCharm.
However in VSCode, when I try to execute the same file I get errors. After investigating it seems that VSCode is picking native Python2 - even though I have the miniforge Python selected. In this picture it can be seen that the status bar at the bottom states Python interpreter selected is Python3. But the output window shows that the python interpreter is Python2.
A more confusing thing is when I use VSCode for Jupyter notebook files then it picks up the interpreter correctly and I have no issues.
I have checked both User and Workspace settings, and they all point to Python3. How can I fix this for standard .py files?
I prefer VSCode to PyCharm, but will need to use PyCharm till this is resolved.
It seems that your system console cannot see python3. You need to put the python3 in the PATH variable, before python2. Like:
PATH=path/to/python3:path/to/python2:$PATH
Also, make sure that the environment containing python3 is activated before command prompt appears. It can be done in bash_profile by adding a line like
conda activate my_env_with_python3
Try changing the settings "Python:Python path", "Python:default interpreter path" and "Python:conda path" also.
I have just bumped into something similar. The Run code option resulted in the file being run with the default interpreter instead of the venv-based one with necessary packages installed.
The fix was simply to use "Run python file" instead:
The run-code behavior must be customizable, something is mentioned e.g. here: Run Code vs Run Python File in Terminal for VSCODE but I didn't bother.

Jupyter notebooks in Visual Studio Code does not use the active virtual environment

I write Python code in Visual Studio Code and run the program from a terminal in which I have activated a virtual environment, and it works fine.
However, if I create notebook cells using #%% and run those interactively, the virtual environment is not used. How can I fix this?
It's because there is an extra step needed - you need to explicitly install a Jupyter kernel that points to your new Python virtual environment. You can't simply activate Jupyter-lab or Notebook from the virtual environment. This has tripped me up before, too.
Follow the advice here: Using Jupyter notebooks with a virtual environment
And, in fact, there can be an issue where your kernel still doesn't point to the correct Python binary, in which case you need to change one suggestion in the above advice process:
From: ipython kernel install --user --name=projectname
To: python3 -m ipykernel install --user --name=projectname
(This correction comes from a comment to Jupyter Notebook is loading incorrect Python kernel #2563.)
*and don't forget to restart VSCode
All you need is to edit Vscode settings following these steps:
Open Open User settings using shortcut Ctrl + Shift + P
Type in search space "env"
Under Extentions -> Python , you will find Python: Venv Path
Type the absolute path to your enviroment "path/to/myenv/bin" in linux or "path/to/myenv/Script/"
Restart vsCode
Select the desired kernel using Notebook : Select Notebook kernel using shortcut Ctrl + Shift + P
Read more here: https://techinscribed.com/python-virtual-environment-in-vscode/
For VSCode, your Jupyter kernel is not necessarily using the same python interpreter you're using at the command line.
Use Ctrl + Shift + P to open the Command Palette, and select "Notebook: Select Notebook Kernel"
Then choose the interpreter you're using at the terminal
I find it easy to use pipenv install ipykernel to set up the virtual environment with the Jupyter kernel in one go (comment on rocksteady's answer).
Encounter the same behaviour. Python code works perfectly fine, but Jupyter refuses to pick up the local .venv.
The local venv is in Python: Select Interpreter but not in Jupyter's Select kernel list.
The problem is there're too many venv in the system!
If you encounter the same behaviour,
Press F1, then Jupyter: Filter kernels, uncheck everything, except the local env.
Then F1 -> Developer: Reload Window.
Jupyter will automatically use the default local venv.
for me solved by adding the path of my venv to the settings.json,
now the kernel is detected automatically
"python.pythonPath": "P:\Miniconda3_64bit\venv\Scripts\python.exe",
Make sure you have installed jupyter, notebook, ipykernel libraries in your virtual environment.
Then hit Ctrl + Shift +P , press >Python: Select Interpreter and choose your path of the venv.
After that, hit Ctrl + Shift +P again, run >Notebook: Select Notebook kernel.
If you have already opened the jupyter notebook window , reload it again and you can find your path of the venv in Jupyter's Select kernel list.
On macOS I have .venv/ in the same folder as my .ipynb
. .venv/bin/activate
pip install ipykernel
Then I restart VSCode in the project folder, open the notebook, and in the Select Kernel dropdown I see .venv/bin/python
Selecting that, now it works.
Here is how to do for venv with Jupyter Notebook on VSCode in Windows:
Create a venv and get the path to this venv in Windows. As an example, with Anaconda, I get: C:\Users\rascoussier\Anaconda3\envs\research310.
Now, we need to tell VSCode to use it. In VSCode, go to the Python Extension > Extension Settings. Search for Python: Venv Path. Add the path where the venvs are located. Here we added C:\Users\rascoussier\Anaconda3\envs\research310.
Restart VSCode.
Now launch command pallet(ctrl+shift+P) and run >Notebook: Select Notebook kernel. Normally the venv python should be available and it should then works.
Try a few things:
Make sure you can run the code from a Visual Studio Code terminal using the "ipython" prompt with the same Conda environment.
If it works then sometimes it is a caching issue, so close your file and open a new one.
Now let me show you a scenario. You select the interpreter in Visual Studio Code, and then you write codes below '# %%'. The moment you hit Ctrl + Enter, you are guessing that the IPython kernel that Visual Studio Code is using is not of the interpreters that you have selected. In this case you could write the following code to conform which interpreter is used for IPython kernel.
import sys
print(sys.executable)
This shows the executable path that the IPython kernel is using. If you see that it's not taking the correct interpreter then here's something that worked for me.
Just restart your computer. Then reopen Visual Studio Code and reselect the interpreter and again hit Ctrl + Enter. Now this time Visual Studio Code should take the correct interpreter and its IPython kernel.
See the final output image
If this happens while using WSL server, don't forget to install Python in the WSL as well, because it doesn't come automatically from the local installation to the server.
https://code.visualstudio.com/docs/remote/wsl-tutorial
Another alternative is to specify the folders where the environmental variables should be sought for.
Create your virtual environment using conda create --name ENV_NAME e.g conda create --name pwd
Then, conda activate pwd
It Should print out details like this:
Use that environment location
Edit Vscode settings following these steps:
Open Open User settings using shortcut Ctrl + Shift + P
Type in search space "env"
Under Extentions -> Python , you will find Python: Venv Folders
(See the image below)
Try installing the Anaconda Extension pack.
When I code in Visual Studio Code with this extension in the bottom left corner, I can select the virtual environment I want to execute my code in. Hence installing this package should make the trick.

Activate conda environment stopped working in vscode

I have noticed that conda activate environment stopped working in vscode integrated terminal with no obvious reason (I certainly did not install any new extensions, nor updated conda or vscode in the last few days).
Expected behaviour
After running conda activate environment I want the environment to be activated and be capable of using the environment python interpreter. Essentially it should look like this:
(base) lpravda-ml:~ lpravda$ conda activate rdkit-env
(rdkit-env) lpravda-ml:~ lpravda$ which python
/Users/lpravda/anaconda3/envs/rdkit-env/bin/python
This is what I can achieve using non-vscode terminal and work with conda as usual.
Actual behaviour
This happens when I type in the same set of commands in vscode integrated terminal:
(base) lpravda-ml:~ lpravda$ conda activate rdkit-env
(rdkit-env) lpravda-ml:~ lpravda$ which python
/usr/local/bin/python
The environment seems to be switched on, however python still points to the system install python 2.7 and I have no idea why.
It used to be working like for years and now it broke down.
Note: When I run debugger in vscode the correct conda environment is selected and executed.
My setup:
OS: Mac OS X: 10.14.3
vscode: 1.31.1
conda: 4.6.7
Update
ongoing issue with the following update:
OS: Mac OS X: 10.14.3
vscode: 1.33.3
conda: 4.6.8
Any idea what could possibly broke down?
it is issue which you can read about it here, but I find this solution
which solve the issue in this version. Also, as community said the problem with python interpreter and env gonna fix in next major update.
Create an workspace folder
Inside the create file post the following code:
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.condaPath": "/home/pcuser/anaconda3/condabin/conda",
"python.pythonPath": "/home/pcuser/anaconda3/envs/my_env/bin/python",
"python.terminal.activateEnvironment": true,
"python.terminal.activateEnvInCurrentTerminal": true,
}
}
Save configuration and close vscode
Open vscode.
It will select automatically the conda enviroment (Should appear at the bottom left of vscode). Inside terminal 'my_env' will also be selected. If vscode doesn't select the 'Python Selected Interpreter' before you open the terminal the my_env inside terminal won't be selected. Open the terminal after the my_env is selected by vscode