Skip to content
 
📑标签
🏷python 🏷env

🗒初墨

🍊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/

升级

升级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

怎么自己管理镜像源

Playlist
Total 4
  • 星茶会
    灰澈
  • song1
    author1
  • soewrewfg1
    author1
  • PIKASONIC - Blossom
    author1