🗒初墨
🍊Hello,各位好,我是面包!
想同时玩转Python 2和3?怕库版本打架?Anaconda用“虚拟环境”让你像切换平行宇宙一样丝滑,附保姆级配置攻略!
打开conda prompt
anaconda双击无反应,连图标也不显示,可能是字符编码格式错误,我遇到的有gdk与utf-8,可以通过命令开启anaconda-nevigator,然后通过终端报错信息打开指定文件修改编码格式即可。
如何创建你的第一个虚拟环境
sh
# 创建虚拟环境
conda create --name env_name python=3.6
# 激活虚拟环境
activate env_name # 激活/使用/进入某个虚拟环境
# 安装python包
conda install xxx=版本号 # 指定版本号指定虚拟环境
sh
activate env_name # 激活/使用/进入某个虚拟环境sh
deactivate # 退出当前环境sh
conda create --name new_env_name --clone old_env_name # 复制某个虚拟环境sh
conda remove --name env_name --all # 删除某个环境查看全局环境
sh
conda info --envs # 查看当前所有环境
conda env list # 查看当前所有环境更新当前环境的python
sh
conda search python # 检查可用的python版本
conda install python=x.x.x # 更新到希望的版本
conda update python # 更新到最新的 Python 版本查看当前虚拟环境下的所有安装包
sh
conda list 需进入该虚拟环境
conda list -n env_name安装或卸载包(进入虚拟环境之后)
sh
conda install requests
conda install xx=版本号 # 指定版本号
conda install xxx -i 源名称或链接 # 指定下载源
conda uninstall xxx分享虚拟环境
sh
conda env export > environment.yml # 导出当前虚拟环境
conda env create -f environment.yml # 创建保存的虚拟环境源服务器管理
conda当前的源设置在$HOME/.condarc中,可通过文本查看器查看或者使用命令>conda config --show-sources查看。
sh
conda config --show-sources #查看当前使用源
conda config --remove channels 源名称或链接 #删除指定源
conda config --add channels 源名称或链接 #添加指定源例如:
sh
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/国内pip源
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
升级
升级Anaconda需先升级conda
sh
conda update conda
conda update anaconda卸载
sh
rm -rf anaconda批量导出虚拟环境中的所有组件
sh
conda list -e > requirements.txt # 导出
conda install --yes --file requirements.txt # 安装pip批量导出环境中的所有组件
sh
pip freeze > requirements.txt
pip install -r requirements.txt国内下载资源需要科学上网,因此我们需要配置国内镜像源
添加清华源
sh
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/添加阿里云镜像源
sh
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/添加中科大源
sh
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/(可选)设置搜索时显示通道地址
sh
conda config --set show_channel_urls yes安装opencv
创建opencv的环境仓库
sh
conda create --name opencv进入opencv的环境仓库
sh
activate opencv安装opencv-python库
sh
opencv-python==3.4.1.15安装opencv-contrib-python库
sh
opencv-contrib-python==3.4.1.15这两个库的版本号需要保持一致
安装jupyter-notebook
sh
pip install jupyter打开jupyter-notebook
sh
jupyter notebook安装matplotlib库
sh
pip install matplotlib安装pytorch
py
import torch
print(torch.__version__) # 2.6.0+cpu
print(torch.cuda.is_available()) # False说明你被conda镜像源坑了,下载了cpu版的torch
怎么自己管理镜像源
