8

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

3
  • the same thing happened with me too. Try reinstalling VS Code. Use the latest 64-bit install, as you have 64-bit python. Maybe consider backing up your settings earlier, using settings-sync. Commented Jan 28, 2021 at 3:35
  • Reinstalling VSCode produced the same exact result. settings-sync did not help either - I was not able to log in using my GitHub. Thank you for your suggestions, but I am still in the middle of this problem. Commented Jan 28, 2021 at 5:56
  • what worked for me was upgrading ipykernel. link Commented Feb 10, 2022 at 18:55

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you Jill. I was able to follow your suggestion, and it worked to make the Python kernel (lower left hand side) match the output of print(sys.version) within the code snippet. However, the package in question, pygmt, is not available from that version. See screen shot in my edited question above. Thus, it is a fork in the road. Do I install pygmt specifically for Python 3.8.5, which seems to be where VSCode is defaulting, or do I try to find out how to actually change VSCode's default to Python 3.9.1? The answer, so far, is neither. I can't do either (see screen shot in edited question)
@brosenheim -In which Python environment is the module "pygmt" installed? and why can't you use the python environment with this module installed? Regarding the default python interpreter, VS Code uses the last selected environment by default. If there is no choice, VS Code will use the first of the python environment variables. Yes, too many python environments are indeed not easy to manage, it is recommended that you try to use virtual envronments.
the module is installed in the python version that is chosen as the interpreter in the first screen shot of my original post - 3.9.1. I cannot install it in 3.8.5, as shown in the 2nd screen shot of my original post. And that is what is frustrating - I know it is there, I am telling VSCode what version to use, and VSCode is not listening to the input. I will look into creating a virtual environment.
@brosenheim -How did you install the module "pygmt" in "Python3.9"? In this thread, we mainly discuss the python version in Jupyter. Is this problem solved?
@brosenheim -In VS Code, we can click on the upper right corner of Jupyter and select the required python kernel.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.