M1开发深度学习的环境搭建

金玉良缘易配而木石前盟难得|M1 Mac os(Apple Silicon)天生一对Python3开发环境搭建(集成深度学习框架Tensorflow/Pytorch)

金玉良缘易配而木石前盟难得|M1 Mac os(Apple Silicon)天生一对Python3开发环境搭建(集成深度学习框架Tensorflow/Pytorch)

    笔者投入M1的怀抱已经有一段时间了,俗话说得好,但闻新人笑,不见旧人哭,Intel mac早已被束之高阁,而M1 mac已经不能用真香来形容了,简直就是“香透满堂金玉彩,扇遮半面桃花开!”,轻抚M1 mac那滑若柔荑的秒控键盘,别说996了,就是007,我们也能安之若素,也可以笑慰平生。好了,日常吹M1的环节结束,正所谓剑虽利,不厉不断,材虽美,不学不高。本次我们尝试在M1 Mac os 中搭建Python3的开发环境。

    一般情况下,直接Python官网(python.org)下载最新的基于arm架构的python3.9即可,但是由于向下兼容等问题,我们尝试使用Python多版本管理软件conda,conda在业界有三大巨头分别是:Anaconda、Miniconda以及Condaforge,虽然都放出消息要适配M1芯片,但是目前最先放出稳定版的是Condaforge,进入下载页面:https://github.com/conda-forge/miniforge/#download  选择mac arm64位架构:

    该文件并不是安装包,而是一个shell脚本,下载成功后,进入命令行目录:

复制
cd ~/Downloads

    执行命令进行安装:

复制
sudo bash ./Miniforge3-MacOSX-arm64.sh

    随后会有一些条款需要确认,这里按回车之后键入yes:

复制
Welcome to Miniforge3 4.9.2-7

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>
BSD 3-clause license
Copyright (c) 2019-2020, conda-forge
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Do you accept the license terms? [yes|no]
[no] >>> yes

    安装的默认版本还是3.9,会附带安装35个基础库,这样就不用我们自己手动安装了:

复制
brotlipy                    0.7.0  py39h46acfd9_1001   installed         
bzip2 1.0.8 h27ca646_4 installed
ca-certificates 2020.12.5 h4653dfc_0 installed
certifi 2020.12.5 py39h2804cbe_1 installed
cffi 1.14.5 py39h702c04f_0 installed
chardet 4.0.0 py39h2804cbe_1 installed
conda 4.9.2 py39h2804cbe_0 installed
conda-package-handling 1.7.2 py39h51e6412_0 installed
cryptography 3.4.4 py39h6e07874_0 installed
idna 2.10 pyh9f0ad1d_0 installed
libcxx 11.0.1 h168391b_0 installed
libffi 3.3 h9f76cd9_2 installed
ncurses 6.2 h9aa5885_4 installed
openssl 1.1.1j h27ca646_0 installed
pip 21.0.1 pyhd8ed1ab_0 installed
pycosat 0.6.3 py39h46acfd9_1006 installed
pycparser 2.20 pyh9f0ad1d_2 installed
pyopenssl 20.0.1 pyhd8ed1ab_0 installed
pysocks 1.7.1 py39h2804cbe_3 installed
python 3.9.2 hcbd9b3a_0_cpython installed
python_abi 3.9 1_cp39 installed
readline 8.0 hc8eb9b7_2 installed
requests 2.25.1 pyhd3deb0d_0 installed
ruamel_yaml 0.15.80 py39h46acfd9_1004 installed
setuptools 49.6.0 py39h2804cbe_3 installed
six 1.15.0 pyh9f0ad1d_0 installed
sqlite 3.34.0 h6d56c25_0 installed
tk 8.6.10 hf7e6567_1 installed
tqdm 4.57.0 pyhd8ed1ab_0 installed
tzdata 2021a he74cb21_0 installed
urllib3 1.26.3 pyhd8ed1ab_0 installed
wheel 0.36.2 pyhd3deb0d_0 installed
xz 5.2.5 h642e427_1 installed
yaml 0.2.5 h642e427_0 installed
zlib 1.2.11 h31e879b_1009 installed

    然后编辑配置文件vim  ~/.zshrc,加入如下内容(此处liuyue是笔者用户名,需改成你的Mac当前用户名):

复制
path=('/Users/liuyue/miniforge3/bin' $path)
export PATH

    存盘之后执行命令:

复制
source ~/.zshrc

    配置好环境变量之后,键入python3:

复制
  ~ python3
Python 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:00:30)
[Clang 11.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

    可以看到已经使用conda安装的python版本了。

    这里简单介绍一下conda命令:

    conda info 可以查看当前conda的基本信息内核,平台,下载源以及目录位置:

复制
  ~ conda info

active environment : None
user config file : /Users/liuyue/.condarc
populated config files : /Users/liuyue/miniforge3/.condarc
conda version : 4.9.2
conda-build version : not installed
python version : 3.9.2.final.0
virtual packages : __osx=11.2.2=0
__unix=0=0
__archspec=1=arm64
base environment : /Users/liuyue/miniforge3 (read only)
channel URLs : https://conda.anaconda.org/conda-forge/osx-arm64
https://conda.anaconda.org/conda-forge/noarch
package cache : /Users/liuyue/miniforge3/pkgs
/Users/liuyue/.conda/pkgs
envs directories : /Users/liuyue/.conda/envs
/Users/liuyue/miniforge3/envs
platform : osx-arm64
user-agent : conda/4.9.2 requests/2.25.1 CPython/3.9.2 Darwin/20.3.0 OSX/11.2.2
UID:GID : 502:20
netrc file : None
offline mode : False

    由于一些众所周知的学术问题,我们需要配置一下国内下载源:

复制
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
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/

conda config --set show_channel_urls yes

    随后查看当前下载源:

复制
conda config --show

    可以看到国内源已经被添加进去了:

复制
channel_priority: flexible
channels:
- https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
- https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
- https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
- https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
- https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
- defaults
- conda-forge
client_ssl_cert: None

    其他的一些conda常用命令:

1. conda --version #查看conda版本,验证是否安装

2. conda update conda #更新至最新版本,也会更新其它相关包

3. conda update --all #更新所有包

4. conda update packag

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值