#包的安装和更新
conda list //查看当前环境中的所有包
conda install XXX (或 pip install XXX) //安装 XXX 包
conda remove XXX (或 pip uninstall XXX) //卸载 XXX 包
#虚拟环境(简称环境)的处理
conda env list:列出所有环境
conda create -n noti python=3 // 创建一个名为noti的环境并指定python版本为3(的最新版本)
activate noti (或 source activate noti) // 激活/切换到noti环境
activate // 切换到base环境
conda env remove -n noti // 删除名称为noti环境
deactivate(或 source deactivate) // 退出环境
#源的处理
conda config --remove-key channels // 换回默认源
例:将环境名为 Python3.7.2 的环境重命名为 py3.7.2:
conda create --name py3.7.2 --clone Python3.7.2 conda remove --name Python3.7.2 --all
其中,
py3.7.2
是新环境名称,Python3.7.2
是需要重命名的旧环境名称。
activate // 切换到base环境
activate learn // 切换到learn环境
conda create -n learn python=3 // 创建一个名为learn的环境并指定python版本为3(的最新版本)
conda env list // 列出conda管理的所有环境
#包的安装和更新、卸载
conda list // 列出当前环境的所有包
conda install requests 安装requests包
conda remove requests 卸载requets包
conda remove -n learn --all // 删除learn环境及下属所有包
conda update requests 更新requests包
#导入导出环境
conda env export > environment.yaml // 导出当前环境的包信息,将包信息存入yaml文件
conda env create -f environment.yaml // 用配置文件创建新的虚拟环境,重新创建一个相同的虚拟环境
pip.ini 是 pip 工具的配置文件,用于指定 pip 的默认配置参数。在 Windows 系统下,pip.ini 文件通常位于用户主目录下(如 C:\Users\username\pip\pip.ini),如果没有该文件,可以手动创建一个。
pip.ini 中可以设置以下参数:
[global]:全局配置,适用于所有的 pip 操作。
[install]:安装时的配置。
[uninstall]:卸载时的配置。
[wheel]:生成 wheel 文件时的配置。
[list]:列出已安装包时的配置。
[search]:搜索包时的配置。
[cache]:缓存设置。
[trusted-host]:指定信任的主机。
例如,以下是 pip.ini 的示例内容:[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple/ timeout = 6000 trusted-host = pypi.tuna.tsinghua.edu.cn [install] use-feature = fast-deps no-cache-dir = true [uninstall] yes = true
该示例中,[global] 部分配置了使用清华镜像站作为默认的包源,超时时间为 6000 秒,以及信任该镜像站的主机名;[install] 部分开启了 fast-deps 特性(快速依赖安装模式)和 no-cache-dir(禁用缓存)选项;[uninstall] 部分开启了 yes 选项(卸载时不需要确认)。
通常配置如下:
[global] timeout = 6000 index-url=http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com
timeout = 6000
语句可以缺省,其他语句不可以缺少。如果原来pip.ini
有其他内容,不要删除,在相应[xxx]
词下添加内容即可。
通过在 pip.ini 文件中指定默认参数,可以在使用 pip 安装、升级和卸载 Python 包时省去重复的命令行参数设置。
推荐用阿里云源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
pip config set install.trusted-host mirrors.aliyun.com
如果set多次,只能保存最后一次set的镜像源。
pip install XXX -i http://pypi.douban.com/simple/ #xxx为包名
pip config list
pip config unset global.index-url
在C:\Users\xx\pip路径删除配置文件pip.ini,即可恢复默认源。
国内源
清华:https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
配置文件.condarc在C:\Users\电脑用户名\.condarc
路径里,该配置文件作用:
示例:
channels: - defaults - anaconda - conda-forge envs_dirs: - C:\Anaconda3\envs pkgs_dirs: - C:\Anaconda3\pkgs ssl_verify: true
该示例文件中的各部分和对应参数的含义如下:
channels
: 指定包源列表,这里的defaults
、anaconda
、conda-forge
分别是三个常用的社区维护镜像站,按顺序依次搜索包。envs_dirs
: 指定 conda 环境的安装路径。多个路径可以用列表的形式指定。pkgs_dirs
: 指定 conda 包的安装路径。多个路径可以用列表的形式指定。ssl_verify
: SSL 证书验证开关,设为true
表示开启验证,设为false
表示禁用验证(不建议)。通过修改
.condarc
文件中的参数,可以调整 conda 命令的默认行为,从而满足自己的需求。例如,通过修改channels
参数指定自己需要使用的包源;或者通过指定envs_dirs
参数可以将所有环境的安装路径指定为同一个目录,从而方便管理和备份。
本文示例是清华源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --remove-key channels
conda config --show channels
或
conda config --show-sources