Python 创建虚拟环境
理论上是:跑python实验,创建虚拟环境是第一步,以能满足自己入门使用需求为主。
1.创建虚拟环境
2.写入jupyter notebook 的kernel
3.安装各类包
4.其他指令
一、创建虚拟环境
虚拟环境的增删改查:
conda create --name newEnv python=xxx # 添加虚拟环境,必须要规定python版本
# newEnv是一个代词可随意替换
conda env remove -n newEnv # 删除虚拟环境
conda env list # 查看所有虚拟环境
新建虚拟环境不加python可能出现的问题,pip出现大量问题
虚拟环境的进退:
conda activate newEnv # 进入虚拟环境
conda deactivate # 退出虚拟环境
效果展现:
二、写入jupyter notebook 的kernel
打开jupyter notebook后,不同的虚拟环境应对应不同的kernel内核。
2.1 创建ipykernel
第一次创建虚拟环境前要查看主环境下是否安装了 ipykernel
conda install ipykernel # 安装ipykernel
再在,虚拟环境中安装 ipykernel (新建完虚拟环境的必要步骤)
#第一步:安装ipykernel
conda install ipykernel
#第二步:命名kernal
python -m ipykernel install --user --name newEnv --display-name "Python(sklearn)"
结果展示:
创建成功jupyter lab为例的表示:
2.2 删除ipykernal
#步骤一:首先要查看已有的ipykernal,判断自己要删除的ipykernal名
jupyter kernelspec list
#步骤二:删除目标ipykernal
jupyter kernelspec remove [环境名称]
2.3 创建ipykernal可能存在的问题及解决方法
1. 创建新的ipykernal步骤执行 python -m ipykernel install --user --name newEnv --display-name "Python(sklearn)"
:
日志报错如下:
AttributeError:type object IOLoop has no attribute initialized
解决方法:
pip install tornado==4.4.3
2. 打开jupyter lab后台日志可能出现的警告如下:
[W 2022-11-13 23:07:41.974 ServerApp] Config option `template_path` not recognized by `ExporterCollapsibleHeadings`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:41.985 ServerApp] Config option `template_path` not recognized by `ExporterCollapsibleHeadings`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:42.053 ServerApp] Config option `template_path` not recognized by `TocExporter`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:42.060 ServerApp] Config option `template_path` not recognized by `TocExporter`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:42.092 ServerApp] Config option `template_path` not recognized by `LenvsHTMLExporter`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:42.103 ServerApp] Config option `template_path` not recognized by `LenvsHTMLExporter`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:42.137 ServerApp] Config option `template_path` not recognized by `LenvsTocHTMLExporter`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:42.148 ServerApp] Config option `template_path` not recognized by `LenvsTocHTMLExporter`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:42.220 ServerApp] Config option `template_path` not recognized by `LenvsLatexExporter`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:42.228 ServerApp] Config option `template_path` not recognized by `LenvsLatexExporter`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 2022-11-13 23:07:42.507 ServerApp] Config option `template_path` not recognized by `LenvsSlidesExporter`. Did you mean one of: `extra_template_paths, template_name, template_paths`?
解决方法:
conda install “nbconvert=5.6.1”
over,虚拟环境创建成功!接下来可以开始导包,构建实验环境了~
三、导包相关
3.1 导入包
- 导包:常用命令
conda install package_name==x.x
pip install package_name==x.x
# 当导入的是下载好的.whl文件时,用pip可能会报错,可以尝试用
pip3 install ".whl文件的绝对路径"
其他:
形如下载sklearn库,要关注下载顺顺序,先下载numpy库,再scipy库,matplotlib库最后再sklearn库。参考https://www.cnblogs.com/python-machine/p/6940028.html
- 加快下载速度,使用
pip
命令时-i 路径
https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ # 清华镜像源
https://pypi.tuna.tsinghua.edu.cn/simple # 清华大学镜像源
http://mirrors.aliyun.com/pypi/simple/ # 阿里云镜像源
http://pypi.douban.com/simple/ # 豆瓣镜像源
3.2 包的删改查
# 查询当前环境所需包
conda list
# 卸载包
conda remove package_name
pip uninstall package_name
# 更新包
conda update package_name
四、其他常用指令
#查询python版本
python --version
参考文献: