Visual Studio Code中的Jupyter笔记本不使用活动虚拟环境 [英] Jupyter notebooks in Visual Studio Code does not use the active virtual environment

查看:91
本文介绍了Visual Studio Code中的Jupyter笔记本不使用活动虚拟环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Visual Studio Code编写Python代码,并从已激活虚拟环境的终端运行该程序,并且运行良好.

I write Python code in Visual Studio Code and run the program from a terminal in which I have activated a virtual environment, and it works fine.

但是,如果我使用#%%创建笔记本单元并进行交互运行,则不会使用虚拟环境.我该如何解决?

However, if I create notebook cells using #%% and run those interactively, the virtual environment is not used. How can I fix this?

推荐答案

这是因为需要执行额外的步骤-您需要显式安装指向您新的Python虚拟环境的Jupyter内核.您不能简单地从虚拟环境中激活Jupyter-lab或Notebook.这也使我震惊了.

It's because there is an extra step needed - you need to explicitly install a Jupyter kernel that points to your new Python virtual environment. You can't simply activate Jupyter-lab or Notebook from the virtual environment. This has tripped me up before, too.

请遵循以下建议: 将Jupyter笔记本与虚拟环境

Follow the advice here: Using Jupyter notebooks with a virtual environment

实际上,可能存在内核 still 不能指向正确的Python二进制文件的问题,在这种情况下,您需要在上述建议过程中更改一个建议:

And, in fact, there can be an issue where your kernel still doesn't point to the correct Python binary, in which case you need to change one suggestion in the above advice process:

来自: ipython kernel install --user --name = projectname

收件人: python3 -m ipykernel install --user --name = projectname

(此更正来自 Jupyter Notebook的评论加载不正确Python内核#2563 .)

(This correction comes from a comment to Jupyter Notebook is loading incorrect Python kernel #2563.)

这篇关于Visual Studio Code中的Jupyter笔记本不使用活动虚拟环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆