对于新用户而言,不清楚如何配置 jupyter notebook 的环境
在 jupyter notebook 切换 conda 环境
1、添加环境
下面演示如何添加环境:
(1)安装相关库
conda install ipykernel
例如:
(python36) gs@aigo:~$ conda install ipykernel
(2)安装环境:
python -m ipykernel install --user --name 环境名称 --display-name "jupyter的环境名称"
例如:
(python36) gs@aigo:~$ conda env list
# conda environments:
#
base /home/gs/anaconda3
gluon /home/gs/anaconda3/envs/gluon
python36 * /home/gs/anaconda3/envs/python36
pytorch /home/gs/anaconda3/envs/pytorch
(python36) gs@aigo:~$
(python36) gs@aigo:~$ python -m ipykernel install --user --name python36 --display-name "python36"
Installed kernelspec python36 in /home/gs/.local/share/jupyter/kernels/python36
(python36) gs@aigo:~$
(3)最后打开jupyter,就可以看到刚刚添加的conda环境了
(python36) gs@aigo:~$ jupyter notebook
2、查看环境
(python36) gs@aigo:~/.local/share/jupyter/kernels$ jupyter kernelspec list
Available kernels:
gluon /home/gs/.local/share/jupyter/kernels/gluon
python36 /home/gs/.local/share/jupyter/kernels/python36
python3_tf /home/gs/.local/share/jupyter/kernels/python3_tf
pytorch /home/gs/.local/share/jupyter/kernels/pytorch
python3 /home/gs/anaconda3/envs/python36/share/jupyter/kernels/python3
(python36) gs@aigo:~/.local/share/jupyter/kernels$
2、修改环境
从上面的日志可以知道,安装的目录为:
(python36) gs@aigo:~/.local/share/jupyter/kernels$ ls
gluon python3 python36 python3_tf pytorch
如果要修改某个配置,或者是修改名字,可以直接修改对应的文件即可,例如:
(python36) gs@aigo:~/.local/share/jupyter/kernels/python36$ cat kernel.json
{
"argv": [
"/home/gs/anaconda3/envs/python36/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "python36",
"language": "python"
}
(python36) gs@aigo:~/.local/share/jupyter/kernels/python36$
3、删除环境
比如删除python3,如下:
(python36) gs@aigo:~/.local/share/jupyter$ jupyter kernelspec remove python3
Kernel specs to remove:
python3 /home/gs/.local/share/jupyter/kernels/python3
Remove 1 kernel specs [y/N]: y
[RemoveKernelSpec] Removed /home/gs/.local/share/jupyter/kernels/python3
查看:
(python36) gs@aigo:~/.local/share/jupyter$ jupyter kernelspec list
Available kernels:
python3 /home/gs/.local/lib/python3.6/site-packages/ipykernel/resources
gluon /home/gs/.local/share/jupyter/kernels/gluon
python36 /home/gs/.local/share/jupyter/kernels/python36
python3_tf /home/gs/.local/share/jupyter/kernels/python3_tf
pytorch /home/gs/.local/share/jupyter/kernels/pytorch
(python36) gs@aigo:~/.local/share/jupyter$
还是有python3,好像没有删除掉,重新打开 jupyter notebook;
在 jupyter notebook 上还能看见python3,这是因为默认会显示,
需要在 jupyter notebook 的配置文件中,添加下面的这句:
c.KernelSpecManager.ensure_native_kernel = False
jupyter notebook 的配置文件,在如下:
gs@aigo:~/.jupyter$ ls
jupyter_notebook_config.py lab migrated nbconfig
最后,再打开 jupyter notebook,python3不见了,完美!
如果没有 jupyter_notebook_config.py
文件,就使用下面的命令生成一个:
$ jupyter notebook --generate-config