Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Jupyter notebook in VSCode does not use the right environment

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 run jupyter lab in such a shell, the code inside the notebooks can import my_env's packages as expected.

If I open my_workspace in VSCode, then my_notebook.ipynb in a tab, my_env is also mentioned in VSCode's status bar ("Python 3.7.6 64-bit ('my_env': conda)"), and my_env is automatically activated when I open a PowerShell prompt in VSCode's console (I ran conda 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 in my_env. When importing a package installed in my_env, but not in base, 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 in my_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?

like image 242
Hey Avatar asked Mar 02 '20 21:03

Hey


People also ask

How do I choose a Jupyter Notebook environment?

You can also create new environments from within Jupyter Notebook (home screen, Conda tab, and then click the plus sign). And you can create a notebook in any environment you want. Select the "Files" tab on the home screen and click the "New" dropdown menu, and in that menu select a Python environment from the list.

How do I change the Python environment in Visual Studio?

Right-click Python Environments in Solution Explorer and select Add Environment. In the Browse dialog that appears, navigate to and select the folder that contains the virtual environment, and select OK. If Visual Studio detects a requirements. txt file in that environment, it asks whether to install those packages.

How do I change the default environment in VS Code?

Select and activate an environment# By default, the Python extension looks for and uses the first Python interpreter it finds in the system path. To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).

Is VS Code good for Jupyter?

Jupyter Notebooks in VS Code. Jupyter (formerly IPython Notebook) is an open-source project that lets you easily combine Markdown text and executable Python source code on one canvas called a notebook. Visual Studio Code supports working with Jupyter Notebooks natively, and through Python code files.


1 Answers

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.

like image 70
pablosjv Avatar answered Oct 22 '22 09:10

pablosjv