更换jupyter notebook的kernel的python环境

更改jupyter notebook的python环境变量
当我们在使用jupyter notebook的时候常常会出现导入的包不存在的情况

import torch

报错

ModuleNotFoundError: No module named 'torch'

原因可能是kernel的环境变量是默认的,不是自己安装的python虚拟环境的,这时候需要更换python环境。
首先在命令行输入jupyter kernelspec list查看jupyter kernels

 python3    d:\anaconda\envs\pytorch\share\jupyter\kernels\python3

进入到当前目录下,打开kernel.json文件

{
 "argv": [
  "D:\\anaconda\\envs\\pytorch\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "python3",
 "language": "python"
}

其中第三行就是kernel的python环境,只需要将该路径更换成自己创建的虚拟环境的python路径即可。
修改完之后,重启jupyter notenook服务,就可以使用。

如果在jupyter notebook的服务器仍然报错为 no kernels,那么可能是因为自己创建的虚拟环境中,没有安装ipykernel
这时候需要在自己创建的虚拟环境中安装ipykernel

pip install ipykernel -i https://pypi.douban.com/simple

使用豆瓣源加速安装,安装完之后,重启jupyter notebook服务,就可正常运行。

参考:https://blog.csdn.net/castle_cc/article/details/86491524
https://www.jianshu.com/p/538c4aa17ba8

你可能感兴趣的:(jupyter,python,编辑器)