Python/Jupyter notebook in VSCode does not use the right environment
I think there is no parameter right now to control that in the settings.json
. I had similar problems with the environments in which the notebook is launched and I was able to fix this modifying the kernelspec
section in the IPython notebook.
Basically, open the notebook as a JSON file and remove the kernelspec
section. When the notebook is launched from vscode, that part will be filled with the default python environment kernel for the workspace. In my case, is filled with the pipenv environment.
Hey
Updated on June 14, 2022Comments
-
Hey almost 2 years
The situation
I use Anaconda 3 on Windows 10.
I have a Visual Studio Code workspace (
my_workspace
) than contains a Jupyter notebook (my_notebook.ipynb
). VSCode has the Python extension installed.The file
my_workspace/settings.json
contains:{ "python.pythonPath": "C:\\Users\\Me\\Anaconda3\\envs\\my_env\\python.exe" }
my_env
is an existing Anaconda environment. I can activate it and work with it in a shell, and if I runjupyter lab
in such a shell, the code inside the notebooks can importmy_env
's packages as expected.If I open
my_workspace
in VSCode, thenmy_notebook.ipynb
in a tab,my_env
is also mentioned in VSCode's status bar ("Python 3.7.6 64-bit ('my_env': conda)"), andmy_env
is automatically activated when I open a PowerShell prompt in VSCode's console (I ranconda init
once).The problem
When the notebook is opened in VSCode, the Jupyter kernel seems to use the
base
environment's Python interpreter instead of the one inmy_env
. When importing a package installed inmy_env
, but not inbase
, I get this error:>>> import keras Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'keras'
This happens for all packages, not just
keras
.In the notebook tab in VSCode, if I click on the interpreter's name in the top-right corner, then choose the correct interpreter (the one in
my_env
), then the notebook runs correctly inmy_env
. But I have to do this every time I re-open VSCode.How to make the default kernel respect the environment chosen in
settings.json
? -
R Chiodo about 4 yearsWe (I work on the extension) try to use the kernelspec in the notebook if it's there. So if the kernelspec references a valid kernel, we'll use that to start the IPython kernel under the covers. Unfortunately as @pablosjv noticed, this doesn't use your selected python environment. It literally just runs the python specified in the kernel.json as if you started it from an empty command shell. However if you erase the kernelspec from the notebook, we'll instead generate a new kernelspec that matches the environment you have selected in VS code. That's why pablosjv fix works.
-
Shai Cohen about 3 yearsstrangely, this also works for c# notebooks! a "language-agnostic" solution should definitely have a special badge.
-
user8491363 about 3 years@RChiodo VSCode Jupyter & Python are wonderful extensions but I think this behavior can be improved by explicitly letting the user know which kernel interpreter it is using. I had to dig around for a while to find out why
import torch
was not working on VSCode's Jupyter when it worked just fine on the native Jupyter env.