Search code examples
pythonvisual-studio-codejupyter-notebookversionenvironment

VSCode issue with Python versions and environments from Jupyter Notebooks


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?).


Solution

  • 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.