简体   繁体   English

来自 Jupyter Notebooks 的 Python 版本和环境的 VSCode 问题

[英]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.问题: Python 的环境和版本与 VSCode 中的设置不匹配,并导致我尝试在 Jupyter 笔记本中使用的包出现问题。 I am using a Windows 10 machine with Python 3.9.1 installed (including older versions), with Visual Studio Code 1.52.1.我正在使用安装了 Python 3.9.1(包括旧版本)和 Visual Studio Code 1.52.1 的 Windows 10 机器。 Short summary - I install a package using pip.简短摘要 - 我使用 pip 安装了 package。 My guess is that it associates with the latest version of Python.我的猜测是它与最新版本的 Python 相关联。 I set up an interpreter in VS Code for that version of python, and try to import the package.我在 VS Code 中为该版本的 python 设置了一个解释器,并尝试导入 package。 The package is not found.未找到 package。 If I call sys.version from the Jupyter notebook, I see that a default version of Python is running (3.8.5).如果我从 Jupyter 笔记本调用 sys.version,我会看到 Python 的默认版本正在运行 (3.8.5)。 The simple notebook throws an error because it cannot find that package that I installed with pip.简单的笔记本会抛出错误,因为它找不到我用 pip 安装的 package。

Screenshot that shows the associations:显示关联的屏幕截图: 带有注释的 VSCode 屏幕截图。 import sys 在笔记本中以蓝色圈出,然后有一个箭头向下显示 print(sys.version) 为 3.8.5 的输出(默认,2020 年 9 月 3 日,21:29:08)。另一个蓝色箭头表示解释器设置为 Python 3.9.1。一组红色箭头显示 import pygmt 命令,红色箭头跟踪调用错误 ModuleNotFoundError: No module named 'pygmt'。另一个红色箭头表示 pygmt 0.2.1 安装在终端的 pip list 命令下

This error is reproducible with only the仅使用

import pygmt

command in the notebook, ie 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?问题:如果更改解释器不起作用,我如何控制 Jupyter 笔记本将在 VSCode 中运行的版本? 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).编辑此编辑是对@Jill Cheng 的回答的回应(请参阅下面的我的评论以获得 tl;dr)。 I can change the interpreter (lower left corner of VSCode) to match the default that VSCode wants to run.我可以更改解释器(VSCode 的左下角)以匹配 VSCode 想要运行的默认值。 No problem.没问题。 屏幕截图显示可以更改解释器(左下角)以匹配默认的 VSCode Python 版本。但是,我无法将所需的包安装到这个版本的 Python 中,如终端所示。 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).我无法将目标 package pygmt 安装到此版本的 Python 中(请参阅终端中的命令)。
  2. I cannot make VSCode operate in the opposite direction - ie I can never get it to run Python 3.9.1 even when that is the interpreter I chose.我不能让 VSCode 以相反的方向运行 - 即我永远无法让它运行 Python 3.9.1,即使那是我选择的解释器。

I have reinstalled VSCode now several times, and Python 3.8.5 seems to always be the default.我现在已经重新安装了几次 VSCode,Python 3.8.5 似乎总是默认的。 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?对我来说,最简单的解决方案似乎是更改默认的 Python 版本的 VSCode - 事实上,这不是选择解释器的重点吗? 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?). 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? )。

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).在VS Code中,Jupyter notebook使用的Python kernel(Python环境)可以独立于VS Code左下角我们选择的Python环境(如图)。

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.如屏幕截图中的 output 所示,您正在使用的 Jupyter 的 Python kernel 是“Python3.8.5”,但此环境中未安装模块“pygm” (Jupyter uses the last selected Python environment by default.) (Jupyter 默认使用上次选择的 Python 环境。)

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

在此处输入图像描述

Reference: Jupyter notebooks in VS Code .参考: VS Code 中的 Jupyter 笔记本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VSCODE JUPYTER - 如何在笔记本(.ipynb)中显示与 VSCode 的 python(.py)中相同的自动完成功能? - VSCODE JUPYTER - How to display SAME AUTOCOMPLETION in notebooks (.ipynb) as it is in python (.py) from VSCode? 命令行 python 和 jupyter notebook 使用两个不同版本的 torch - Command line python and jupyter notebooks use two different versions of torch 无法在 VScode 中打开 Python:Select 解释器或 Jupyter 笔记本 - Cannot open Python:Select Interpreter nor Jupyter notebooks in VScode 如何为 VSCode Jupyter 笔记本保存默认的 python env - How to save a default python env for VSCode Jupyter Notebooks VSCode 中的 Jupyter 笔记本未显示最新的 Python 内核列表 - Jupyter notebooks in VSCode not showing up to date python kernel list 如何在 jupyter 笔记本(VSCode + Python 扩展)中隐藏单元格 output? - How to hide cell output in jupyter notebooks (VSCode + Python Extension)? Jupyter问题与python / pyspark版本 - Jupyter issue with python / pyspark versions Pylance 在 VSCode Jupyter 笔记本中不起作用 - Pylance not working in VSCode Jupyter notebooks 在 Jupyter Notebooks 中定义 KneighborsClassifier 的问题 - Issue defining KneighborsClassifier in Jupyter Notebooks 在 VScode Jupyter Notebooks 中使用字典打印正确的制表符 - Print correct tabspaces with dictionaries in VScode Jupyter Notebooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM