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

标签 python visual-studio visual-studio-code jupyter-notebook conda

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

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

(automl-meta-learning) brandomiranda~/automl-meta-learning ❯ which python
/Users/brandomiranda/miniconda3/envs/automl-meta-learning/bin/python

还从 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

但是当我尝试 jupyter 笔记本时它不起作用。

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

enter image description here

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

enter image description here

当我尝试在 jupyter 笔记本中运行内容时,出现以下错误:

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


.

这很奇怪。我的 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.

但似乎没有什么区别。

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

<小时/>

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

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

但我无法确定他们到底想要我做什么。

如何解决这个问题?

<小时/>

相关资源:

最佳答案

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

  1. 打开命令面板

    Mac:CMD+Shift+P

    电脑:CTRL+SHIFT+P

  2. 然后搜索select Interpreter to start jupyter server然后按回车键,它将列出所有解释器,然后选择任何解释器即可完成!

it's just a one time process, after this, it will get connected automatically.

关于python - 如果 Jupyter 内核没有自动连接,如何将其连接到 VS Code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59867847/

相关文章:

python - 登录网站后使用 PyCurl 获取请求

python - 用循环更新表

visual-studio - 如何导航回 Visual Studio 中的最后一个光标位置?

python - 制作元组的笛卡尔不起作用

python - 如何在Python中从shell中获取变量?

windows - 如何从命令提示符打开 Visual Studio 解决方案文件

c# - 如何将 WCF 客户端代理生成的类中的方法标记为虚拟

visual-studio-code - TSLint 没有隐藏变量,在哪里可以关闭它?

visual-studio-code - VS Code - 如何使向下箭头向下移动而不是导航建议

visual-studio-code - 如何在 VS Code 中打开的选项卡之间切换