[英]Python/Jupyter notebook in VSCode does not use the right environment
I use Anaconda 3 on Windows 10.我在 Windows 10 上使用 Anaconda 3。
I have a Visual Studio Code workspace ( my_workspace
) than contains a Jupyter notebook ( my_notebook.ipynb
).我有一个 Visual Studio Code 工作区 (
my_workspace
),而不是一个 Jupyter 笔记本 ( my_notebook.ipynb
)。 VSCode has the Python extension installed. VSCode 安装了 Python 扩展。
The file my_workspace/settings.json
contains:文件
my_workspace/settings.json
包含:
{
"python.pythonPath": "C:\\Users\\Me\\Anaconda3\\envs\\my_env\\python.exe"
}
my_env
is an existing Anaconda environment. my_env
是现有的 Anaconda 环境。 I can activate it and work with it in a shell, and if I run jupyter lab
in such a shell, the code inside the notebooks can import my_env
's packages as expected.我可以激活它并在 shell 中使用它,如果我在这样的 shell 中运行
jupyter lab
,则笔记本中的代码可以按预期导入my_env
的包。
If I open my_workspace
in VSCode, then my_notebook.ipynb
in a tab, my_env
is also mentioned in VSCode's status bar ("Python 3.7.6 64-bit ('my_env': conda)"), and my_env
is automatically activated when I open a PowerShell prompt in VSCode's console (I ran conda init
once).如果我在
my_notebook.ipynb
中打开my_workspace
,然后在选项卡中打开 my_notebook.ipynb,在my_env
的状态栏中也会提到 my_env(“Python 3.7.6 64-bit ('my_env': conda)”),并且打开时会自动激活my_env
VSCode 控制台中的 PowerShell 提示符(我运行了一次 conda conda init
)。
When the notebook is opened in VSCode, the Jupyter kernel seems to use the base
environment's Python interpreter instead of the one in my_env
.当笔记本在 VSCode 中打开时,Jupyter kernel 似乎使用
base
环境的 Python 解释器而不是my_env
中的解释器。 When importing a package installed in my_env
, but not in base
, I get this error:导入安装在
my_env
中但未安装在base
中的 package 时,出现此错误:
>>> import keras
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'keras'
This happens for all packages, not just keras
.这发生在所有包上,而不仅仅是
keras
。
In the notebook tab in VSCode, if I click on the interpreter's name in the top-right corner, then choose the correct interpreter (the one in my_env
), then the notebook runs correctly in my_env
.在 VSCode 的笔记本选项卡中,如果我单击右上角的解释器名称,然后选择正确的解释器(
my_env
中的解释器),则笔记本在my_env
中正确运行。 But I have to do this every time I re-open VSCode.但是每次我重新打开 VSCode 时都必须这样做。
How to make the default kernel respect the environment chosen in settings.json
?如何使默认 kernel 尊重 settings.json 中选择的
settings.json
?
I think there is no parameter right now to control that in the settings.json
.我认为现在没有参数来控制
settings.json
。 I had similar problems with the environments in which the notebook is launched and I was able to fix this modifying the kernelspec
section in the IPython notebook.我在启动笔记本的环境中遇到了类似的问题,我能够通过修改 IPython 笔记本中的
kernelspec
部分来解决这个问题。
Basically, open the notebook as a JSON file and remove the kernelspec
section.基本上,将笔记本作为 JSON 文件打开并删除
kernelspec
部分。 When the notebook is launched from vscode, that part will be filled with the default python environment kernel for the workspace.当笔记本从 vscode 启动时,该部分将填充工作区的默认 python 环境内核。 In my case, is filled with the pipenv environment.
在我的情况下,充满了 pipenv 环境。
In my case, after creating the new Environment the VS Code had to be relaunched several times and then it found the new env.就我而言,在创建新环境后,必须多次重新启动 VS 代码,然后才能找到新的环境。 It looked as if VS Code had to take some time to find the new Env in the system.
看起来 VS Code 必须花一些时间才能在系统中找到新的 Env。
maybe this helps, but maybe not.也许这有帮助,但也许没有。 I ran into this issue during a ssh session on a remote machine.
我在远程机器上的 ssh session 期间遇到了这个问题。 I had to use the VS code command > Python: Select Interpreter
我不得不使用 VS 代码命令 > Python: Select 解释器
form here I needed to add the location to the environment I wished to work form.在这里形成我需要将位置添加到我希望工作的环境中。
so long as jupyter is installed in the path there should be no issues (:只要在路径中安装了 jupyter 就应该没有问题(:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.