Tom Hale
Tom Hale

Reputation: 46775

VSCode Jupyter loads incorrect version of python

VSCode's Jupyter isn't actually running the version of python that it displays in the lower left of the screen.

Below, it purports to be running 3.9.1, but the output of the cell shows that it is indeed running 3.7.9.

I selected the displayed rl environment via: Select environment to start Jupyter Server.

What doesn't work:

incorrect version of python

Extra info:

Python Output:

> conda --version
> pyenv root
> python3.7 ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3.6 ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3 ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python2 ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> ~/.local/share/miniconda3/envs/rl/bin/python ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> conda info --json
Starting Pylance language server.
Python interpreter path: ~/.local/share/miniconda3/envs/rl/bin/python
> conda env list
> conda env list

Yes, that last listed interpreter really is v3.9.1:

% ~/.local/share/miniconda3/envs/rl/bin/python --version
Python 3.9.1

Jupyter Output:

User belongs to experiment group 'jupyterTest'
> ~/.local/share/miniconda3/envs/rl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py -c "import ipykernel"
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py -c "import notebook"
> ~/.local/share/miniconda3/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py -c "import ipykernel"
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py -c "import jupyter"
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py jupyter kernelspec --version
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.jupyter_daemon -v
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.jupyter_daemon -v
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.jupyter_daemon -v
> ~/.local/share/miniconda3/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_launcher_daemon -v
> ~/.local/share/miniconda3/envs/rl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_launcher_daemon -v
Started kernel Python 3
> ~/.local/share/miniconda3/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_launcher_daemon -v

This final listed python is the incorrect, non-selected version:

% ~/.local/share/miniconda3/bin/python --version
Python 3.7.9

Why is this version being used when the correct ~/.local/share/miniconda3/envs/rl/bin/python is listed immediately above?

Upvotes: 3

Views: 3525

Answers (4)

Don M
Don M

Reputation: 986

I've been in situations where I've tried to specify the python version when creating the environment for conda.

conda env create -f environment.yml python={version}

It would usually install the latest version and ignore my command.

enter image description here

To resolve this I activate my environment: conda activate ${env}

Then I install the right version: conda install python=3.9.12 -y

The key or big take away that I gathered is in the VSCode UI it gives me the option to select my kernel environment & it also gives me the path to that environment. When I went in to that directory the python version was not there. So...install it, reboot VScode.

Upvotes: 0

user16459765
user16459765

Reputation: 11

https://www.it-swarm-vi.com/vi/python/ipynb-nhap-tap-tin-ipynb-khac/1042793988/

!pip install import_ipynb`enter code here`
!pip install ipynb

import import_ipynb
import ipynb

from ipynb.fs.full.tim_folder import tim_folder

Upvotes: 0

Jill Cheng
Jill Cheng

Reputation: 10354

In VSCode, the Python environment of Jupyter notebook is independent, it uses the Python environment we chose last time by default. We can click on "Python3: Idle" in the upper right corner and switch to the Python3.9 environment.

Please reload VSCode after switching the Python environment to make Jupyter reload the corresponding kernel.

enter image description here

Upvotes: 2

Tom Hale
Tom Hale

Reputation: 46775

What eventually worked for me was:

  1. Close VSCode
  2. In .ipynb pane's top right, change version of Python to the desired conda environment
  3. Start the kernel

Changing the kernel and then restarting it didn't seem to work.

Upvotes: 3

Related Questions