用Anaconda管理python的库环境

install Anaconda

just need to install miniconda

linux or mac
wget https://docs.conda.io/en/latest/miniconda.html
bash Miniconda3-latest-Linux-x86_64.sh
source .bashrc
环境只在你当前用户有效,如/home/dxx,在用户dxx中安装,那么所有annconda环境就只在当前dxx用户下有效,包括后来用conda安装其他包,或者用pip安装其他包。

用Anaconda管理python的库环境

  • 使用pip install python==3.7.0是错的,pip没有存这样的包,想改python版本只能通过conda来改conda install python=3.7.0
  • pip的默认源:https://pypi.org/simple/
  • 默认是base环境,基本所有的包都预装好了
  • 创建新环境
    • conda create -n testname python==3.7,testname为环境名, -n代表创建新环境的名字,可以用conda create -h, 查看其他的命令参数
      -n ENVIRONMENT, --name ENVIRONMENT
                        Name of environment.
    
    • conda activate testname , 转换到testname这个环境中,新建立的环境装的包很少,如numpy包都没有,需要自己安装

    • conda deactivate 返回到base环境中

    • conda list 查看当前环境的所有的包
      在这里插入图片描述

    • conda env list 查看所有的环境
      在这里插入图片描述

    • conda env remove -n testname 删除testname环境

    • 转换到了testname环境,就可以用pip install -r requirements.txt 来安装所需要的库环境,requirements.txt长这样, 具体可以通过pip install -h查看其它参数用途

    -r, --requirement <file>    Install from the given requirements file. This option can be used multiple times.
    

在这里插入图片描述

安装pytorch

https://pytorch.org/get-started/locally/

换源

- 在当前环境下安装各种库,有需要改源加快下载安装速度,出错可以多试试更改不同的源
# 豆瓣
https://pypi.doubanio.com/simple/
在用清华的源的时候安装tensorflow总是出现大串红字错误,换了豆瓣的源再安装就没错了

# 阿里云    
https://mirrors.aliyun.com/pypi/simple/
# 清华大学
https://pypi.tuna.tsinghua.edu.cn/simple/
https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/

# 临时设置
pip install some-package -i https://mirrors.aliyun.com/pypi/simple/	

# 永久更改源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 取消更改源/改回默认的pip源
pip config unset global.index-url

# 或者是, 速度很慢,但是所有的包都有
pip config set global.index-url https://pypi.org/simple/

pip 版本问题

需要用pip -V查看自己的pip版本,pip20.3的版本不好,在你安装不同包的版本时,源里没有,他不会提示你其它可安装的版本,所以需要用conda install pip==20.1.1,安装20.1的版本,具体效果如下:

在这里插入图片描述

ERROR: Could not find a version that satisfies the requirement opencv-python==3.4 (from versions: 3.1.0.5, 3.2.0.6, 3.2.0.7, 3.2.0.8, 3.3.0.9, 3.3.0.10, 3.3.1.11, 3.4.0.12, 3.4.0.14, 3.4.1.15, 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.5.20, 3.4.6.27, 3.4.7.28, 3.4.8.29, 3.4.9.31, 3.4.9.33, 3.4.10.35, 3.4.10.37, 3.4.11.39, 3.4.11.41, 3.4.11.43, 3.4.11.45, 4.0.0.21, 4.0.1.23, 4.0.1.24, 4.1.0.25, 4.1.1.26, 4.1.2.30, 4.2.0.32, 4.2.0.34, 4.3.0.36, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46)
ERROR: No matching distribution found for opencv-python==3.4

在这里插入图片描述

(testVersion2) PS C:\Users\pickg> pip install opencv-python==3.4
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
ERROR: Could not find a version that satisfies the requirement opencv-python==3.4
ERROR: No matching distribution found for opencv-python==3.4

而且pip最新版本从官网的源很多的包也搜不到,包括你在官网手动下载whl文件手动安装,也会报错,说不支持whl文件的安装。其中testVesion的pip是20.1.1,testVersion的pip版本是20.3.3 , 结果如下
在这里插入图片描述

或者在创建新环境的时候直接指定pip版本,而不用最新的版本conda create -n testVersion3 python==3.7 pip==20.1.1 ,创建成功后,可以用pip -V查看自己的版本是否在正确。

关于environment.yml文件,这个文件中包含了创建的新环境的名字,和所需要安装的版本库,conda env create -f environment.yml

–file FILE Read package versions from the given file. Repeated file specifications can be passed (e.g.–file=file1 --file=file2).

就长这样
在这里插入图片描述
但是个人觉得,还是一个一个库安装比较妥当,因为你不知道安装到哪个库,就出错了,你还要找到,并且在文件中指定pip版本也是个问题,按照上面的命令,pip默认还是会安装最新版本。总而言之,因为源和强的原因,还是手动安装比较好。

anaconda + vscode

更具体的环境vscode官网

在vscode中按Ctrl+Shift+P或者是F1,用Select Interpreter就能找到conda的不同的环境
在这里插入图片描述
如我选择的是conda中的gluon环境(环境需要自己去创建,默认的只有base)
在这里插入图片描述
在这里插入图片描述
左下角显示的是当前用的环境,可以点击下次就能切换环境。

mac下的anaconda

2021年2月27日,有了第一台MacBook(i5),记录下下载anaconda与安装。

在这里插入图片描述
去官网找这两个版本,上面的上图形界面安装,下面的上在命令行利用bash安装,如下载好sh文件
bash ~/Downloads/Anaconda3-5.3.1-MacOSX-x86_64.sh
执行它一步步安装

“In order to continue the installation process, please review the license agreement.”(“请浏览许可证协议以便继续安装。”),点击“Enter”查看“许可证协议”

“Press Enter to confirm the location, Press CTRL-C to cancel the installation or specify an alternate installation directory.”(“按回车键确认安装路径

“Do you wish the installer to prepend the Anaconda install location to PATH in your /home//.bash_profile ?”
这一步上添加conda的环境变量

执行source,更新环境变量,就可以用conda命令了
source ~/.bash_profile

与windows下不同的是,激活新的环境用的是source activate base

手动添加环境变量,是在.bash_profile文件里添加
export PATH="/Users/用户名/anaconda3/bin:$PATH"

参考:https://zhuanlan.zhihu.com/p/51137150

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值