VSCode issue with Python versions and environments from Jupyter Notebooks

Question:

Issue: I am having issues with the environment and version of Python not matching the settings in VSCode, and causing issues with the packages I am trying to use in Jupyter notebooks. I am using a Windows 10 machine with Python 3.9.1 installed (including older versions), with Visual Studio Code 1.52.1 . Short summary – I install a package using pip. My guess is that it associates with the latest version of Python. I set up an interpreter in VS Code for that version of python, and try to import the package. The package is not found. If I call sys.version from the Jupyter notebook, I see that a default version of Python is running (3.8.5). The simple notebook throws an error because it cannot find that package that I installed with pip.

Screenshot that shows the associations:
Screen capture from VSCode with annotations. import sys is circled in blue in the notebook, and an arrow follows that down to an output showing the print(sys.version) is 3.8.5 (default, Sep 3 2020, 21:29:08). Another blue arrow shows that the interpreter is set to Python 3.9.1. A set of red arrows shows the import pygmt command, with red arrows tracing that call to error ModuleNotFoundError: No module named 'pygmt'. Another red arrow shows that pygmt 0.2.1 is installed under a pip list command in terminal

This error is reproducible with only the

import pygmt

command in the notebook, i.e. it does not depend on the other packages imported.

Question: How can I control the version that Jupyter notebooks will run in VSCode if changing the interpreter doesn’t work? Every other issue like this that I have found was due to not choosing the correct interpreter. In this case, I have. Why is it not running that version of the interpreter?

Thank you.

Edit
This edit is in response to @Jill Cheng’s answer (see my comment below for tl;dr). I can change the interpreter (lower left corner of VSCode) to match the default that VSCode wants to run. No problem. Screen shot showing that interpreter (lower left corner) can be changed to match the default VSCode Python version. However, I cannot install the needed package into this version of Python, as seen in terminal.
But there still are problems.

  1. I cannot install pygmt, the targeted package, into this version of Python (see the command in in the terminal).
  2. I cannot make VSCode operate in the opposite direction – i.e. I can never get it to run Python 3.9.1 even when that is the interpreter I chose.

I have reinstalled VSCode now several times, and Python 3.8.5 seems to always be the default. To me, the easiest solution seems to be changing the default Python version of VSCode – in fact, isn’t that the point of selecting an interpreter? It is more nebulous to me why I cannot install pygmt into Python 3.8.5, and I don’t know if it is beneficial to have multiple different versions of Python all with different packages (or is this just what Python users deal with daily?).

Asked By: brosenheim

||

Answers:

In VS Code, the Python kernel (Python environment) used by Jupyter notebook can be independent of the Python environment we selected in VS Code (shown in the lower left corner of VS Code).

As the output in the screenshot shows, the Python kernel of Jupyter you are using is "Python3.8.5", but the module "pygmt" is not installed in this environment. (Jupyter uses the last selected Python environment by default.)

Solution: Click the Python kernel on the upper right in the Jupyter notebook, and select the python environment where the module "pygmt" has been installed. In addition, it is recommended that you reopen the jupyter file after switching Jupyter’s Python kernel so that it can reload the new python kernel.

enter image description here

Reference: Jupyter notebooks in VS Code.

Answered By: Jill Cheng

If I understand this question correctly, I’m struggling with this with VScode, but the terminal and jupyter notebook are using the same kernel:

from a jupyter notebook

print(sys.version)
3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]

sys.path
['/home/sam/working/gate/SPECT',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '',
 '/home/sam/.local/lib/python3.10/site-packages',
 '/usr/local/lib/python3.10/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/home/sam/.local/lib/python3.10/site-packages/IPython/extensions',
 '/home/sam/.ipython']

from a terminal within VScode using IPython

In [2]: sys.path

Out[2]: 
['/usr/local/bin',
 '/home/sam/devel/build/INSTALL/python',
 '//home/sam/devel/build/INSTALL/python',
 '//home/sam/devel/STIR/STIR_github/STIR/src/swig',
 '//home/sam/working/Standalone_MIC/src',
 '//home/sam/working/src',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '',
 '/home/sam/.local/lib/python3.10/site-packages',
 '/usr/local/lib/python3.10/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/home/sam/.local/lib/python3.10/site-packages/IPython/extensions',
 '/home/sam/.ipython']

In [3]: print(sys.version)
3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]

I don’t understand why these would be different.

Any help would be much appreciated

Sam

Answered By: samd