jupyter-notebook 配置 anaconda 中的虚拟 python 环境

210 阅读1分钟

1.anaconda 安装 nb_conda_kernels 可以搜索 anaconda 下的所有 ipython 内核

  conda install nb_conda_kernels

2.创建自己的虚拟环境名为 python36 ,使用的是 python 3.6 的环境

conda create -n python36 python=3.6 

3.激活自己的虚拟环境

conda activate python36

4.现在已经进入虚拟环境中(终端的前缀会多显示虚拟环境名出来),在虚拟环境中安装 ipykernel ,如果中间需要其他包自行安装

pip install ipykernel

5.然后将该虚拟环境展示到 jupyter 中, --name 后跟的是自己的虚拟环境名,--display-name 后面 跟着的是想在 jupyter 中显示的名字,这个可以自己定义,如果不加这个参数,默认内核和虚拟环境命名一样

python -m ipykernel install --user --name python36 --display-name "python36"

6.重启 jupyter ,刷新页面即可看到 New 按钮下面会多出来 Python[conda env:python36] 的环境

7.如果想退出虚拟环境,执行

conda deactivate

8.如果想删除某个虚拟环境

conda remove -n tf_1.15.0 --all

9.如果想删除某个内核

~/anaconda3/bin/jupyter kernelspec remove python36

另外

中间在执行第 3 步之前可能会要求执行 conda initialize ,这个操作会将 bash_profile 中配置好的 anaconda 路径覆盖掉,并自动在 bash_profile 中添加如下内容,这会造成终端前缀始终有虚拟环境名,如果执行 conda deactivate 一直退出,那就只能执行笔记本中的 python 环境了。

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/wys/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/wys/opt/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/Users/wys/opt/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/wys/opt/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<