【问题标题】:VSCode Jupyter loads incorrect version of pythonVSCode Jupyter 加载错误版本的 python
【发布时间】:2020-12-30 08:11:52
【问题描述】:

VSCode 的 Jupyter 实际上并没有运行它在屏幕左下方显示的 python 版本。

下面,它声称运行的是 3.9.1,但单元格的输出显示它确实运行的是 3.7.9。

我通过Select environment to start Jupyter Server选择了显示的rl环境。

什么不起作用:

  • 重新启动 Jupyter 内核
  • 选择不同的环境(无论环境的 python 是什么,它们实际上都运行 3.7.9)

额外信息:

Python 输出:

> conda --version
> pyenv root
> python3.7 ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3.6 ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3 ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python2 ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> ~/.local/share/miniconda3/envs/rl/bin/python ~/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> conda info --json
Starting Pylance language server.
Python interpreter path: ~/.local/share/miniconda3/envs/rl/bin/python
> conda env list
> conda env list

是的,最后列出的解释器确实是 v3.9.1:

% ~/.local/share/miniconda3/envs/rl/bin/python --version
Python 3.9.1

Jupyter 输出:

User belongs to experiment group 'jupyterTest'
> ~/.local/share/miniconda3/envs/rl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py -c "import ipykernel"
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py -c "import notebook"
> ~/.local/share/miniconda3/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py -c "import ipykernel"
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py -c "import jupyter"
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py jupyter kernelspec --version
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.jupyter_daemon -v
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.jupyter_daemon -v
> ~/.local/share/miniconda3/envs/nndl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.jupyter_daemon -v
> ~/.local/share/miniconda3/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_launcher_daemon -v
> ~/.local/share/miniconda3/envs/rl/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_launcher_daemon -v
Started kernel Python 3
> ~/.local/share/miniconda3/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2020.12.414227025/pythonFiles/pyvsc-run-isolated.py vscode_datascience_helpers.daemon --daemon-module=vscode_datascience_helpers.kernel_launcher_daemon -v

最后列出的这个 python 是不正确的、未选择的版本:

% ~/.local/share/miniconda3/bin/python --version
Python 3.7.9

当正确的~/.local/share/miniconda3/envs/rl/bin/python 直接在上面列出时,为什么要使用这个版本?

【问题讨论】:

    标签: python python-3.x visual-studio-code jupyter-notebook


    【解决方案1】:

    最终对我有用的是:

    1. 关闭 VSCode
    2. .ipynb 窗格的右上角,将 Python 版本更改为所需的 conda 环境
    3. 启动内核

    更改内核然后重新启动它似乎不起作用。

    【讨论】:

      【解决方案2】:

      在VSCode中,Jupyter notebook的Python环境是独立的,它默认使用我们上次选择的Python环境。我们可以点击右上角的“Python3: Idle”,切换到Python3.9环境。

      切换Python环境后请重新加载VSCode,使Jupyter重新加载对应的内核。

      【讨论】:

      • 很遗憾,这是不正确的。扩展 Python 依赖于 Jupyter:Cannot disable extension 'Jupyter'. Extension 'Python' depends on this. 我现在在屏幕的右上角和左下角都有 3.9.1,但 sys.version 输出保持不变。
      • 为指出右上角的部分干杯,但它导致了我的答案。
      【解决方案3】:

      https://www.it-swarm-vi.com/vi/python/ipynb-nhap-tap-tin-ipynb-khac/1042793988/

      !pip install import_ipynb`enter code here`
      !pip install ipynb
      
      import import_ipynb
      import ipynb
      
      from ipynb.fs.full.tim_folder import tim_folder
      

      【讨论】:

        猜你喜欢
        • 2021-09-23
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        • 2021-06-16
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        • 2020-12-10
        相关资源
        最近更新 更多