目的:将利用conda自定义的python虚拟环境添加到jupyter notebook中
- 创建虚拟环境:
conda create -n snp python=3.8.3
,并切换到当前虚拟环境下conda activate snp
。 - 安装ipykernel包:
conda install ipykernel
- 添加到Jupyter中:
python -m ipykernel install --name snp
#后面的环境名字需要更换 - 重启Jupyter,即可看到添加的虚拟环境
- 切换环境,测试代码(一直显示“服务正在启动,请等待”)的字样,查看命令行发现问题ImportError: DLL load failed while importing error: 找不到指定的模块。进行解决。详细的错误代码如下:
[I 21:59:57.053 NotebookApp] KernelRestarter: restarting kernel (4/5), new random ports
Traceback (most recent call last):
File "D:\Software\Anaconda3\envs\snp\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\Software\Anaconda3\envs\snp\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\ipykernel_launcher.py", line 15, in <module>
from ipykernel import kernelapp as app
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\ipykernel\__init__.py", line 2, in <module>
from .connect import *
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\ipykernel\connect.py", line 16, in <module>
import jupyter_client
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\jupyter_client\__init__.py", line 4, in <module>
from .connect import *
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\jupyter_client\connect.py", line 21, in <module>
import zmq
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\zmq\__init__.py", line 55, in <module>
from zmq import backend
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\zmq\backend\__init__.py", line 40, in <module>
reraise(*exc_info)
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\zmq\utils\sixcerpt.py", line 34, in reraise
raise value
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\zmq\backend\__init__.py", line 27, in <module>
_ns = select_backend(first)
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\zmq\backend\select.py", line 28, in select_backend
mod = __import__(name, fromlist=public_api)
File "D:\Software\Anaconda3\envs\snp\lib\site-packages\zmq\backend\cython\__init__.py", line 6, in <module>
from . import (constants, error, message, context,
ImportError: DLL load failed while importing error: 找不到指定的模块。
原因:pyzmq版本不匹配问题,需要卸载旧的,在虚拟环境安装新的版本
解决方法:在虚拟环境下pip uninstall pyzmq
,然后pip install pyzmq
成功:虚拟环境添加成功。(虽然还是同样的版本)