参考:在 Jupyter Notebook 和 Python 中使用虚拟环境 - njanakiev
windows 上
这里不借助 conda 及其其它 python 版本管理工具,就是自己通过 python 命令或 pycharm 创建的一个 python 虚拟环境,我暂且命名为 env-py11,它在我的 E 盘。
以下是在 win 上实现,mac,linux,conda 等见上面链接里的方法。
# 前提
pip install --user ipykernel
# 查看当前 jupyter 所有的环境
(env-py311) C:\Users\xulon\Desktop>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:
python3 D:\Python\Python311\share\jupyter\kernels\python3
# 添加虚拟环境 env-py311 到 jupyter
(env-py311) C:\Users\xulon\Desktop>python -m ipykernel install --user --name=env-py311
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.
Installed kernelspec env-py311 in C:\Users\xulon\AppData\Roaming\jupyter\kernels\env-py311
# 查看
(env-py311) C:\Users\xulon\Desktop>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:
env-py311 C:\Users\xulon\AppData\Roaming\jupyter\kernels\env-py311
python3 D:\Python\Python311\share\jupyter\kernels\python3
# 但此时 env-py311 的实际并不是你想要的那个环境,它只是一个名字
# list 查看 env-py11 在 jupyter 里的位置,修改kernel.json,吧argv里的路径换成你自己的虚拟环境的位置
# 再运行 jupyter-lab 或者 jupyter notebook
# 切换“内核”为自己的虚拟环境即可
# 删除指定环境
jupyter kernelspec uninstall env-py311