如果没有自动连接,如何将 Jupyter 内核连接到 VS Code?

Cha*_*ker 7 python visual-studio conda visual-studio-code jupyter-notebook

我希望能够有一个开箱即用的工作 jupyter 笔记本,用于 VS 代码,而我这边的工作最少。

我尝试打开一个 jupyter 笔记本。我立即找到了解释器并将我的 conda env 与命令颗粒一起使用(command + shift + P然后在下拉菜单中找到了我的 conda env)。这似乎使终端工作,因为哪个 python 指向正确的位置:

(automl-meta-learning) brandomiranda~/automl-meta-learning ? which python
/Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python
Run Code Online (Sandbox Code Playgroud)

也从 VS Code 运行 python 脚本似乎也工作正常,请参阅输出:

(automl-meta-learning) brandomiranda~/automl-meta-learning ? /Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python /Users/brandomiranda/automl-meta-learning/python_playground.py

x = 1
my_str = this is a string
y = 2
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用 jupyter notebook 时它不起作用。

最明显的是我的 VS 代码没有连接内核,看截图:

在此处输入图片说明

我尝试按照 reddit 上的建议点击几个箭头(vs_code_jupyter_server_no_kernel_python_not):

在此处输入图片说明

当我尝试在我的 jupyter notebook 中运行东西时,我收到以下错误:

Error: Activating Python 3.7.6 64-bit ('base': conda) to run Jupyter failed with Error: StdErr from ShellExec, /Users/brandomiranda/.bashrc: line 31: jump-module.bash: No such file or directory

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init 

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


.
Run Code Online (Sandbox Code Playgroud)

这很奇怪。我的 shell 甚至不是 bash,所以我不知道为什么会向我显示该消息(也许 VS Code 不知道 Mac OS Catalina 使用 zsh?不确定如何为 VS Code 修复该问题)。无论如何,我继续做我认为错误消息所暗示的事情。所以我做了以下事情:

我运行 conda init 然后进入 vs code 中的终端,并通过运行 zsh 重新启动 shell,但 jupyter 内核仍然无法在 vs code 中的笔记本上运行。不知道出了什么问题。

此外,我做了错误消息建议的操作:

(automl-meta-learning) brandomiranda~/automl-meta-learning ? conda init zsh
no change     /Users/brandomiranda/miniconda3/condabin/conda
no change     /Users/brandomiranda/miniconda3/bin/conda
no change     /Users/brandomiranda/miniconda3/bin/conda-env
no change     /Users/brandomiranda/miniconda3/bin/activate
no change     /Users/brandomiranda/miniconda3/bin/deactivate
no change     /Users/brandomiranda/miniconda3/etc/profile.d/conda.sh
no change     /Users/brandomiranda/miniconda3/etc/fish/conf.d/conda.fish
no change     /Users/brandomiranda/miniconda3/shell/condabin/Conda.psm1
no change     /Users/brandomiranda/miniconda3/shell/condabin/conda-hook.ps1
no change     /Users/brandomiranda/miniconda3/lib/python3.7/site-packages/xontrib/conda.xsh
no change     /Users/brandomiranda/miniconda3/etc/profile.d/conda.csh
no change     /Users/brandomiranda/.zshrc
No action taken.
Run Code Online (Sandbox Code Playgroud)

但似乎没有什么区别。

知道如何解决这个问题吗?


VS code python 扩展的开发人员建议我遵循他们在这里所做的一切:

https://github.com/microsoft/vscode-python/issues/9566

但我无法弄清楚他们到底想让我做什么。

我该如何解决?


相关资源:

Ale*_*x L 5

看起来很奇怪,前几天我也注意到了这一点,到目前为止唯一对我有用的是通过从 Anaconda Navigator 启动它来打开 VS Code:

在此处输入图片说明

然后我得到了,正如预期的那样:

在此处输入图片说明


Ank*_*mar 5

您需要为jupyter选择python解释器,您可以按照以下步骤进行

  1. 打开命令面板

    苹果电脑: CMD+Shift+P

    个人电脑: CTRL+SHIFT+P

  2. 然后搜索select Interpreter to start jupyter server然后回车,它会列出所有的解释器,然后选择任何解释器就完成了!

这只是一个一次性过程,在此之后,它将自动连接。

  • 仅供参考,我必须重新启动 vscode 才能生效 (5认同)
  • 不,它不会要求我“选择解释器来启动 jupyter 服务器”。“连接到 Jupyter 服务器”和“安装 Python”只是两个选项。我已经手动安装了Python。我不信任 Windows 11 应用中心的 Python。 (2认同)