参考 | Anaconda环境下安装jupyter内核
需求
conda create -n py38(这个是你新建环境的名字) python=3.8
在新建了一个环境后, 想在 jupyter notebook 里面用这个环境运行代码, 就要在这个环境下装一个 jupyter notebook 内核
步骤
-
激活新建的环境: 打开 Anaconda prompt
conda activate py38(新建环境的名字)
-
执行以下命令安装 ipykernel:
conda install ipykernel
如果执行这条命令遇到以下情况:Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source. Collecting package metadata (repodata.json): done Solving environment: done
则先执行:
conda clean -a
清空不必要的缓存
然后
conda install ipykernel
重新安装 ipykernel
-
完成第二步后, 继续在当前环境命令窗口下, 执行下面命令:
python -m ipykernel install --user --name mydemo(jupyter内核名称) --display-name mydemo(jupyter内核名称)
其中mydemo
更换成你想命名的 jupyter 内核名称就好, 我自己是改成py38Jupyter
执行这一步可能会出现很多报错, 这里列举几个我见到的:- ImportError: cannot import name ‘generator_to_async_generator’
解决办法:
pip uninstall -y ipython prompt_toolkit
pip install ipython prompt_toolkit
- ModuleNotFoundError: No module named ‘IPython.core’
解决办法:
pip uninstall ipython
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ipython
- ImportError: cannot import name ‘generator_to_async_generator’
-
然后打开 jupyter notebook, 随便打开一个 ipynb 文件, 按照下图指示就可以切换内核
假如在切换过程中, 一直提示 内核正在启动,请等待, 然后提示错误内核连接失败
这个时候就有必要打开刚刚打开 jupyter notebook 弹出来的黑色小窗口, 看看有没有提示下面错误(图源来自知乎):
提示错误: AttributeError:type object IOLoop has no attribute initialized
解决办法: 给 tornado 降级
pip uninstall tornado
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tornado==4.4.3