conda常用命令,创建环境、激活环境、删除环境、conda换源、pip换源等
conda下载 https://www.anaconda.com/download 点此跳转
文章目录
- conda常用命令,创建环境、激活环境、删除环境、conda换源、pip换源等
- 1.查看conda版本
- 2.创建conda环境
- 2.1创建一个新的环境
- 2.2创建一个指定Python版本的新环境
- 3.使用conda管理环境
- 3.1列出所有conda环境
- 3.2激活conda环境
- 3.3退出当前环境
- 3.4删除一个环境
- 4.使用conda管理环境中的包
- 4.1列出当前环境的所有包
- 4.2安装一个包
- 4.3更新或移除一个包
- 4.4导出环境依赖或从环境依赖创建一个环境
- 5.conda换源
- 5.1查看conda使用的源
- 5.2换成国内源
- 5.2.1清华源
- 5.2.2 中科大源
- 5.2.3其他国内源
- 5.3切回默认源
- 6.pip换源
- 6.1 临时换源
- 6.2 全局换源
- 6.3其他国内源
- 7.conda或pip清理包缓存
1.查看conda版本
conda --version
2.创建conda环境
2.1创建一个新的环境
conda create --name envname
其中envname是你创建的环境名称
2.2创建一个指定Python版本的新环境
conda create --name envname python=x.x
其中envname是你创建的环境名称,x.x是你想要安装的Python版本
3.使用conda管理环境
3.1列出所有conda环境
conda env list
3.2激活conda环境
conda activate envname
其中envname是你想要激活的环境名称
3.3退出当前环境
conda deactivate
3.4删除一个环境
conda env remove --name envname
其中envname是你想要删除的环境名称
4.使用conda管理环境中的包
4.1列出当前环境的所有包
#使用conda命令
conda list
#使用pip命令
pip list
查看pip的版本
pip --version
4.2安装一个包
#使用conda命令
conda install package_name
#使用pip命令
pip install package_name
其中package_name是你想要安装的包名
安装指定版本的包
#使用conda命令
conda install package_name=x.x
#使用pip命令
pip install package_name==x.x
x.x是你想要安装包的版本,这里需要注意一下,conda命令安装指定版本包名是一个=而使用pip命令是两个=
4.3更新或移除一个包
更新一个包
#使用conda命令
conda update package_name
#使用pip命令
pip install --upgrade package_name
移除一个包
#使用conda命令
conda remove package_name
#使用pip命令
pip uninstall package_name
4.4导出环境依赖或从环境依赖创建一个环境
导出环境的依赖项列表
#使用conda命令
conda list --export > requirements.txt
#使用pip命令
pip freeze > requirements.txt
其中requirements.txt是你导出环境的依赖项列表的文本文件
使用上面的命令导出的依赖项中的包可能会出现本地路径安装,这对传递给其他用户使用时非常不便的,下面的两个命令会在导出依赖项时屏蔽本地路径
#在 macOS、Linux 或类 Unix 系统中,你可以使用 grep 筛选掉包含本地文件路径的行
pip freeze | grep -v "file:///" > requirements.txt
#在 Windows 中,你可以使用 findstr 来执行操作
pip freeze | findstr /V /C:"file:///" > requirements.txt
使用conda命令导出.yml后缀文件
conda env export > environment.yml
从环境依赖项列表文件创建一个环境
#使用pip命令
pip install -r requirements.txt
requirements.txt是你想要导入环境依赖的文本文件
使用.yml后缀依赖项创建环境
conda env create -f environment.yml
这将根据 .yml 文件创建一个新的 Conda 环境,并安装所列出的所有依赖项
如果你已经有一个存在的环境,并且想要根据新的 .yml 文件更新它:
conda env update -f environment.yml --prune
这将更新环境中的软件包,并删除在 .yml 文件中不存在的软件包
conda env update -f environment.yml
不使用 --prune将会保留你原来环境中已经存在的包,不会删除
5.conda换源
5.1查看conda使用的源
conda config --show channels
没换之前是默认源
5.2换成国内源
5.2.1清华源
# 添加清华镜像源
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 --set show_channel_urls yes
使用下面查看源是否添加成功
conda config --show channels
5.2.2 中科大源
# 添加中科大镜像源
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 --set show_channel_urls yes
查看是否成功
5.2.3其他国内源
中山大学镜像源:
conda config --add channels https://mirrors.sustech.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.sustech.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
华中科技大学镜像源:
conda config --add channels https://mirrors.hust.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.hust.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
北京外国语大学镜像源:
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
5.3切回默认源
conda config --remove-key channels
6.pip换源
6.1 临时换源
pip install "packagename" -i https://pypi.tuna.tsinghua.edu.cn/simple
packagename是下载的包名,这种方法每下载都需要用一次,-i后面是是你需要换国内源的网址
6.2 全局换源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
这样下载源就换成国内源,以后下在就不需要再换源了
6.3其他国内源
#阿里云
http://mirrors.aliyun.com/pypi/simple/
#豆瓣
http://pypi.douban.com/simple/
#中国科学技术大学
http://pypi.mirrors.ustc.edu.cn/simple/
#华中科技大学
http://pypi.hustunique.com/
#腾讯云
https://mirrors.cloud.tencent.com/pypi/simple/
#华为云
https://mirrors.huaweicloud.com/repository/pypi/simple/
7.conda或pip清理包缓存
conda命令
#使用 conda 的 clean 子命令来清除缓存包:
conda clean --all
#这将删除包缓存、索引缓存、未使用的包和 tarballs。
#你也可以使用以下选项来精确控制清理内容:
--packages 或 -p:#只清除包缓存
--source-cache 或 -s:#只清除索引缓存
--tarballs 或 -t:#只清除 tarballs
--index-cache 或 -i:#只清除索引缓存
--locks:#删除所有的 conda 锁文件
--tempfiles:#删除所有的 conda 临时文件
pip命令
pip cache purge