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

查看:30
本文介绍了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.

遵循此处的建议:使用带有虚拟环境

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

而且,事实上,可能存在一个问题,您的内核仍然没有指向正确的 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.)

*并且不要忘记重新启动 VSCode

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

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