代码之家  ›  专栏  ›  技术社区  ›  Charlie Parker

如果不自动连接,如何将Jupyter内核连接到VS代码?

  •  1
  • Charlie Parker  · 技术社区  · 5 年前

    我希望能够有一个现成的jupyter笔记本来处理VS代码,而我只需要做最少的工作。

    我试着打开一个jupyter笔记本。我立即找到了解释器,并将我的conda env与命令pellet一起使用( command + shift + P 然后在下拉菜单中找到我的conda环境)。这似乎使终端工作,因为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_cernel_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代码中的终端,并通过运行zsh重新启动了shell,但jupyter内核在vs代码中仍然无法在我的笔记本上运行。不知道怎么了。

    此外,我按照错误消息的建议进行了操作:

    (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代码python扩展的开发人员建议我遵循他们在这里所做的一切:

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

    但我不知道他们到底想让我做什么。

    我该怎么解决这个问题?


    相关资源:

    0 回复  |  直到 5 年前
        1
  •  17
  •   Ankit Kumar    5 年前

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

    1. 打开命令面板

      雨衣: CMD+Shift+P

      PC: CTRL+SHIFT+P

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

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

        2
  •  8
  •   Alex L    5 年前

    尽管看起来很奇怪,但前几天我也注意到了这一点,到目前为止,对我来说唯一有效的方法是从Anaconda Navigator启动VS Code:

    enter image description here

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

    enter image description here

        3
  •  1
  •   Charlie Parker    5 年前

    如果你看到我在git问题上进行的长时间讨论( https://github.com/microsoft/vscode-python/issues/9566 )一旦我删除了我的所有错误 .bashrc .zhrc ,VS代码中的jupyter功能再次开始为我工作。这太奇怪了(特别是因为我根本不使用bash,我使用zsh作为我的shell,我本来希望VS代码对我的 .SHELLrc 文件抛出错误,但事实并非如此)。

    如果这不起作用,那么您可能需要安装vsix视图扩展并安装 ms-python-insiders.vsix 在问题中链接( https://github.com/microsoft/vscode-python/issues/9566 ).

        4
  •  0
  •   jimh    3 年前

    如果在Windows VSCode上,对我有效的方法是安装和启用Python和Jupyter扩展,那么 CTRL+SHIFT+P , select Interpreter to start jupyter server 这些扩展是瓶颈。