创建虚拟环境
在Anaconda Prompt中输入:conda create -n your_env_name python=X.X即可创建新的虚拟环境,其中your_env_name是自定义的虚拟环境名,X.X用于指定虚拟环境的python编译器版本,如:3.7、3.8等等。
关于虚拟环境的相关代码如下:
#查看虚拟环境
conda env list
#创建虚拟环境
conda create -n your_env_name python=X.X
#激活虚拟环境
conda activate your_env_name
#关闭(退出)虚拟环境
conda deactivate
#删除虚拟环境
conda remove -n your_env_name --all
#查看虚拟环境中所有库文件
conda list
#更新所有库文件
conda update --all
#安装库文件
conda install package
#更新库文件
conda update package
一般情况打开jupyter
1.在Anaconda Prompt的base环境下输入:jupyter notebook,点击new,发现只有python3,并没有自己创建的虚拟环境。
2.创建新文件,发现Kernel中也没有自创建的虚拟环境,只有Python3。
此时,退出虚拟jupyter,用下面方法将虚拟环境加载到jupyter中。
虚拟环境加载到jupyter中
1.进入自建虚拟环境中。如,输入conda activate Pytorch进入自建的Pytorch环境中。
2.安装ipykernel,并将虚拟环境加入jupyter
- 自建虚拟环境中输入:
pip install ipykernel
- 安装完ipykernel后,将虚拟环境加入内核:
python -m ipykernel install --name=your_env_name(虚拟环境名)
当输出下面这个内容,表示将虚拟环境成功加入内核中:Installed kernelspec web_env in C:\ProgramData\jupyter\kernels\your_env_name
3.测试虚拟环境是否被加到了jupyter中
- 虚拟环境中安装一个没装过的包,以django为例
pip install django
- 查看django是否装上
pip list
可以看到装好了
- 退出虚拟环境,到base环境下
conda deactivate
- 查看jupyter中的所有内核
jupyter kernelspec list
红箭头所示,其实已经说明jupyter中已有了虚拟环境“Pytoch”
- base环境下输入jupyter notebook,打开jupyter,并创建新文件
new新文件时,发现此时Pytorch虚拟环境已加入到了jupyter中了,直接点击Pytorch便可创建Pytoch虚拟环境下的jupyter文件
同时,Kernel中看到了虚拟环境Pytorch
导入创建的包django,测试说明了此jupyter文件是在自建虚拟环境中的
删除内核
1.首先,查看目前的内核
jupyter kernelspec list
2.输入下面代码即可删除内核
jupyter kernelspec remove your_env_name(虚拟环境名)
此时,就会删除虚拟环境,并且jupyter中的内核也被删除了(注意:此删除内核操作不仅仅是将内核从jupyter中删除了,对应的虚拟环境也没有了,所以一定确保此虚拟环境是真的不需要了)
jupyter打开指定路径下的.ipynb文件
1.打开jupyter的一般方式
base环境下,输入jupyter notebook即可打开jupyter,new新文件即可。
2.指定路径下的.ipynb文件用jupyter打开
打开某.ipynb文件路径,输入cmd,便可弹出cmd界面框,在cmd中输入jupyter notebook即可打开指定路径的jupyter。
(如有问题,欢迎指出,欢迎讨论和交流,常总结,常复盘!!!)