在jupyter notebook中切换conda虚拟环境

我目前有一个需求,就是在环境中安装了两个虚拟环境,分别是jupyter环境和flask环境,jupyer环境python是3.10,flask环境python是3.11,我想在jupyter lab界面中进行环境的切换。那么我们就需要进行这些操作来实现这个需求。conda环境的安装和虚拟环境的创建我们这里不再赘述,可以查看我之前的笔记:https://sulao.cn/post/1069

我们先去jupyter环境部署jupyter notebook。

conda activate jupyter
conda install notebook
conda install -c conda-forge jupyterlab-language-pack-zh-CN
jupyter notebook --version
7.3.2

将虚拟环境添加为 Jupyter 的内核,以便在 Notebook 中使用:

conda install ipykernel
ipython kernel install --user --name=jupyter-py310

然后我们还需要给jupyter配置一个密码,lab页面无密码避免暴漏在公网,之前老的笔记生成密码的方式跟现在不一样了,这里需要注意下。

jupyter server password

上述操作完成以后我们就可以启动jupyter notebook了。

jupyter notebook --ip=0.0.0.0 --port=8888 --notebook-dir=/worker/ --no-browser --allow-root

然后我们退出当前jupyter的虚拟环境,登录另外一个flask的虚拟环境也像上面操作

deactivateconda activate flaskconda install ipykernelipython kernel install --user --name=flask-py311

然后我们退出flask虚拟环境,进入jupyter虚拟环境中,重启下jupyter,然后打开ip:8888这个地址进行验证

202507081039191897462202.png

可以看到flask和jupyter这两个环境直接可以在jupyterlab上进行切换了。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/1078

评论列表

0%