问题:安装了cv库,但是在jupyter notebook提示No module named 'cv2'
原因:jupyter notebook 默认内核为系统conda,非虚拟环境下的内核
查看Jupyter Notebook可用内核,可在cmd中输入:
jupyter kernelspec list
返回信息
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
Available kernels:
kernelname C:\Users\Administrator\AppData\Roaming\jupyter\kernels\kernelname
python3 D:\TEST\ANACONDA3\share\jupyter\kernels\python3
1,cmd下查看系统默认python内核版本
import sys
sys.executable
进入hack虚拟环境下查看python内核版本
.hack/venv/script/.activate
import sys
sys.executable
为虚拟环境添加jupyter notebook内核
先在虚拟环境中安装ipykernel包(内核管理工具包)
pip install ipykernel
Jupyter notebook 添加或删除内核
1、切换到要添加的虚拟环境,确认是否安装 ipykernel
python -m ipykernel –version
如果没有安装,则安装:
python -m pip install ipykernel
2、为 Jupyter notebook 添加内核
python -m ipykernel install --user --name=kernelname --display-name hack
其中,kernelname为创建的文件夹名,showname为在Jupyter notebook展示的内核名
创建成功后,提示:
C:\Users\Administrator\AppData\Roaming\jupyter\kernels\kernelname
3、查看 Jupyter notebook kernel
jupyter kernelspec list
4、删除 jupyter 内核
jupyter kernelspec remove kernelname
补充:jupyter kernelspce 命令
No subcommand specified. Must specify one of: ['list', 'install', 'uninstall', 'remove', 'install-self', 'provisioners']
Manage Jupyter kernel specifications.
Subcommands
===========
Subcommands are launched as `jupyter kernelspec cmd [args]`. For information on
using subcommand 'cmd', do: `jupyter kernelspec cmd -h`.
list
List installed kernel specifications. ---列出已安装的内核规范
install
Install a kernel specification directory. ---安装内核规范目录。
uninstall
Alias for remove ---移除的别名
remove
Remove one or more Jupyter kernelspecs by name. ---按名称删除一个或多个Jupyter内核规范
install-self
[DEPRECATED] Install the IPython kernel spec directory for this Python. ---[已弃用]为这个Python安装IPython内核规范目录。
provisioners
List available provisioners for use in kernel specifications. ---列出内核规范中可用的配置程序。