总结conda或者pip常见命令
2022年03月11日19:38:01
个人建议学习python最好使用anaconda+pycharm,自己从2020大四初学python以来,到慢慢熟悉conda/pip命令,基本的会用,平时也会把常见的记在记事本中,但是往往总结得不是很全,一般用什么都会baidu。因此,此时用博客将我平日使用到的基础命令记录下来。
助力大家熟练使用anaconda命令行
本文包含
- 下载链接
- 使用常见的conda/pip命令
- anaconda创建虚拟环境
- 添加/切换镜像源加速
- 安装opencv
- 安装pytorch
- 一键安装环境配置requirements.txt
下载
官方下载:https://www.anaconda.com/products/distribution
官网历史版本:https://repo.anaconda.com/archive/
离线安装:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
注意
——下载安装的时候有个path选项记得全部勾选(自动配置环境变量)
Anaconda创建虚拟环境
创建虚拟环境:yida_cv是我虚拟环境的名字,你取什么名字都OK,最好能够标记好环境。
conda create -n yida_cv python=3.6
激活/切换虚拟环境
conda activate yida_cv
退出并进入base环境
# 方法一
conda deactivate
# 方法二
conda activate base
查看已有的虚拟环境
conda env list
删除虚拟环境
conda remove -n yida_cv --all
修改镜像源
参考Anaconda镜像源相关命令
[由于网络限制使用默认镜像源下载包速度有限,所以最好先切换到清华源]
1.查看当前使用的源
conda config --show-sources
2.切换清华镜像源
如何使用
第一步:先把下面的全部内容复制到txt中去
第二步:全部复制到命令行
第三步:回车
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
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/r
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/msys2/
conda config --set show_channel_urls yes
3.还原为默认镜像
conda config --remove-key channels
4.指定源使用
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
# 用腾讯源,快得很,豆瓣都不行了
pip install -i https://mirrors.cloud.tencent.com/pypi/simple numpy
更改默认源
# 腾讯云可以
pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple
# 阿里云也不错
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
源
我最常用的是豆瓣源,能很轻松记得住。
-i http://pypi.douban.com/simple # 豆瓣
-i https://pypi.tuna.tsinghua.edu.cn/simple # 清华
-i http://mirrors.aliyun.com/pypi/simple # 阿里
# 安装numpy使用豆瓣源加速
pip install numpy -i http://pypi.douban.com/simple
记忆小技巧:【 -i 、 py pi、 豆瓣官网、simple】
查看已安装的包
已有的包
conda list
查看指定包
conda list numpy
pip安装/卸载包
安装包:numpy就是包的名字,输入你需要安装的包名
pip install numpy
安装指定版本的包
pip install numpy==1.19.5
安装包时指定镜像源(豆瓣)加速
pip install numpy -i https://pypi.douban.com/simple
卸载包
pip uninstall numpy
更新包
pip install --upgrade numpy
常见包的安装
安装OpenCv
Win10安装
pip install opencv-python
pip install opencv-contrib-python
Mac安装
pip3 install opencv-python --user
安装tensorflow
conda install tensorflow-gpu==2.0.0
安装Pytorch
cpu
版本
conda install pytorch torchvision cpuonly -c pytorch
安装pytorch-gpu
版本:pytorch gpu安装教程(Perfect完美系列)
pytorch官方版本查看
:https://pytorch.org/get-started/previous-versions/
离线下载网址
:https://download.pytorch.org/whl/torch_stable.html
# 方法1:需要网络稳定, 直接从官网下载 官网有直接的下载命令
# conda
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 -c pytorch
# 方法2:安装whl离线torch包
pip install pytorch.whl
# 方法3:具体操作看上面的gpu安装教程
一键安装环境配置requirements.txt
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
如何导出requirements.txt
基于pipreqs导出当前文件夹全部使用的包
pip install pipreqs
pipreqs ./ --encoding=utf-8
# 覆盖
pipreqs ./ --encoding=utf-8 --force
安装torchvision时避免自动安装pytorch
pip install --no-deps torchvision==0.4.0
# --no-deps:这个参数告诉 pip 不要安装任何依赖。
其它命令
启动jupyter
jupyter notebook