Mac系统Homebrew安装Anaconda3,并配置环境变量

说明

  1. Anaconda指的是一个开源的Python发行版本,包括了python和很多常见的软件库, 和一个包管理器conda。常见的科学计算类的库都包含在里面了,使得安装比常规python安装要容易。
  2. Anaconda是专注于数据分析的Python发行版本,包含了conda、Python等190多个科学包及其依赖项。

第一步,安装Homebrew

安装方法参考:juejin.cn/post/719808...

Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。 简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。

第二步,安装Anaconda

执行命令brew search anaconda

sql 复制代码
~:brew search anaconda
==> Casks
anaconda                                           pycharm-ce-with-anaconda-plugin                    pycharm-with-anaconda-plugin

再执行brew install anaconda进行安装

vbnet 复制代码
~:brew install anaconda
Running `brew update --auto-update`...
Installing from the API is now the default behaviour!
You can save space and time by running:
  brew untap homebrew/cask
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
==> New Casks
proton-mail

You have 18 outdated formulae installed.

==> Downloading https://raw.githubusercontent.com/Homebrew/homebrew-cask/dd54274384f2bcb08f89bbf104968ff4cd1f3b9d/Casks/a/anaconda.rb
curl: (7) Failed to connect to raw.githubusercontent.com port 443 after 1 ms: Couldn't connect to server

Trying a mirror...
==> Downloading https://formulae.brew.sh/api/cask-source/anaconda.rb
################################################################################################################################################# 100.0%
==> Downloading https://repo.anaconda.com/archive/Anaconda3-2024.02-1-MacOSX-x86_64.sh
################################################################################################################################################# 100.0%
==> Installing Cask anaconda
==> Running installer script 'Anaconda3-2024.02-1-MacOSX-x86_64.sh'
Password:
Sorry, try again.
Password:
Sorry, try again.
Password:
PREFIX=/usr/local/anaconda3
Unpacking payload ...

Installing base environment...


Downloading and Extracting Packages: ...working... done

Downloading and Extracting Packages: ...working... done
Preparing transaction: ...working... done
Executing transaction: ...working...


    Installed package of scikit-learn can be accelerated using scikit-learn-intelex.
    More details are available here: https://intel.github.io/scikit-learn-intelex

    For example:

        $ conda install scikit-learn-intelex
        $ python -m sklearnex my_application.py



done
installation finished.
==> Changing ownership of paths required by anaconda with sudo; the password may be necessary.
🍺  anaconda was successfully installed!

此时已安装成功,可通过指令brew list查看brew下的目录,可以看到在Casks中有anaconda

perl 复制代码
brew list
==> Formulae
autoconf	fontconfig	graphite2	libgpg-error	libx11		little-cms2	openjpeg	python@3.12	webp
automake	freetype	harfbuzz	libksba		libxau		lz4		openssl@1.1	readline	xorgproto
brotli		fribidi		icu4c		libnghttp2	libxcb		lzo		openssl@3	ruby@3.0	xz
c-ares		gettext		jpeg-turbo	libpng		libxdmcp	m4		pango		sqlite		zlib
ca-certificates	giflib		leptonica	libtiff		libxext		macos-term-size	pcre2		telnet		zstd
cairo		glib		libarchive	libtool		libxrender	mpdecimal	pixman		tesseract
coreutils	gmp		libb2		libuv		libyaml		node@14		pkg-config	tesseract-lang

==> Casks
anaconda

第三步,配置Shell环境变量

1. zsh的配置文件.zshrc(转载:juejin.cn/post/719808...

macOS Catalina(10.15) 开始,Mac 使用 zsh 作为默认shell,而它的配置文件是用户目录下的.zshrc文件,所以我们之前在定义环境变量时都会编辑这个文件。每次打开终端时都会读取这个配置文件,如果需要在当前的shell窗口读取最新的环境配置则需要执行source ~/.zshrc,这也是之前我们编辑该文件后重载配置的原因

2. 添加anaconda的环境变量

.zshrc文件中添加export ANACONDA="/usr/local/anaconda3/bin"(这是使用brew方式安装anaconda3的默认路径,其他方式安装的话anaconda3的路径需要自己找),其中定义ANACONDA全局变量指向anaconda3的所在路径,并在export PATH中引用$ANACONDA,写好后的效果如下:

bash 复制代码
export ANACONDA="/usr/local/anaconda3/bin"
# 写入环境变量
export PATH=$ANACONDA:$RUBY:$GEMS:$JAVA_HOME/bin:$FLUTTER/bin:$PATH
3. 执行source ~/.zshrc指令让系统重新加载环境配置文件
4. 执行conda info指令,显示conda信息则表示安装和配置没问题
yaml 复制代码
conda info

     active environment : None
       user config file : /Users/jiameng/.condarc
 populated config files : /Users/jiameng/.condarc
          conda version : 24.1.2
    conda-build version : 24.1.2
         python version : 3.11.7.final.0
                 solver : libmamba (default)
       virtual packages : __archspec=1=skylake
                          __conda=24.1.2=0
                          __osx=10.16=0
                          __unix=0=0
       base environment : /usr/local/anaconda3  (writable)
      conda av data dir : /usr/local/anaconda3/etc/conda
  conda av metadata url : None
           channel URLs : https://repo.anaconda.com/pkgs/main/osx-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/osx-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /usr/local/anaconda3/pkgs
                          /Users/jiameng/.conda/pkgs
       envs directories : /usr/local/anaconda3/envs
                          /Users/jiameng/.conda/envs
               platform : osx-64
             user-agent : conda/24.1.2 requests/2.31.0 CPython/3.11.7 Darwin/23.3.0 OSX/10.16 solver/libmamba conda-libmamba-solver/24.1.0 libmambapy/1.5.6 aau/0.4.3 c/NL_dHOp842FjGtEJbEUDyQ s/M-rzKOPLMS3BpqWu-ypsLQ e/X-jZUVvB7rJRsp6YfJDR1A
                UID:GID : 501:20
             netrc file : None
           offline mode : False
5. 执行conda env list指令可以看到anaconda有个默认名为"base"的Python环境
bash 复制代码
conda env list
# conda environments:
#
base                     /usr/local/anaconda3

【至此,使用brew安装anaconda已经完成,下面介绍如何使用anaconda创建不同版本的Python环境】

第四步,可创建多个可共存的不同版本的Python环境

通过conda env list指令可以看到anaconda有个默认名为"base"的Python环境,现在我们来创建一个python3.8版本的环境,并且将该环境命名为myenv,执行conda create -n myenv python=3.8指令进行创建:

bash 复制代码
conda create -n myenv python=3.8
Channels:
 - defaults
Platform: osx-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /usr/local/anaconda3/envs/myenv

  added / updated specs:
    - python=3.8


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    pip-23.3.1                 |   py38hecd8cb5_0         2.6 MB
    python-3.8.18              |       h5ee71fb_0        14.4 MB
    setuptools-68.2.2          |   py38hecd8cb5_0         950 KB
    wheel-0.41.2               |   py38hecd8cb5_0         109 KB
    xz-5.4.6                   |       h6c40b1e_0         373 KB
    ------------------------------------------------------------
                                           Total:        18.4 MB

The following NEW packages will be INSTALLED:

  ca-certificates    pkgs/main/osx-64::ca-certificates-2023.12.12-hecd8cb5_0
  libcxx             pkgs/main/osx-64::libcxx-14.0.6-h9765a3e_0
  libffi             pkgs/main/osx-64::libffi-3.4.4-hecd8cb5_0
  ncurses            pkgs/main/osx-64::ncurses-6.4-hcec6c5f_0
  openssl            pkgs/main/osx-64::openssl-3.0.13-hca72f7f_0
  pip                pkgs/main/osx-64::pip-23.3.1-py38hecd8cb5_0
  python             pkgs/main/osx-64::python-3.8.18-h5ee71fb_0
  readline           pkgs/main/osx-64::readline-8.2-hca72f7f_0
  setuptools         pkgs/main/osx-64::setuptools-68.2.2-py38hecd8cb5_0
  sqlite             pkgs/main/osx-64::sqlite-3.41.2-h6c40b1e_0
  tk                 pkgs/main/osx-64::tk-8.6.12-h5d9f67b_0
  wheel              pkgs/main/osx-64::wheel-0.41.2-py38hecd8cb5_0
  xz                 pkgs/main/osx-64::xz-5.4.6-h6c40b1e_0
  zlib               pkgs/main/osx-64::zlib-1.2.13-h4dc903c_0


Proceed ([y]/n)? y


Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate myenv
#
# To deactivate an active environment, use
#
#     $ conda deactivate

此时再执行conda env list可以看到我们新建的环境了:

bash 复制代码
conda env list
# conda environments:
#
base                     /usr/local/anaconda3
myenv                    /usr/local/anaconda3/envs/myenv

来到myenv环境的路径下可以看到确实是python3.8版本:

第五步,VSCode + Anaconda搭建Python环境

重启VSCode打开一个项目,点击右下角的python版本就能看到刚才创建的anaconda环境,点击某个环境即可切换

相关推荐
湫ccc1 小时前
《Python基础》之字符串格式化输出
开发语言·python
mqiqe2 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql
AttackingLin2 小时前
2024强网杯--babyheap house of apple2解法
linux·开发语言·python
哭泣的眼泪4082 小时前
解析粗糙度仪在工业制造及材料科学和建筑工程领域的重要性
python·算法·django·virtualenv·pygame
湫ccc3 小时前
《Python基础》之基本数据类型
开发语言·python
drebander4 小时前
使用 Java Stream 优雅实现List 转化为Map<key,Map<key,value>>
java·python·list
威威猫的栗子4 小时前
Python Turtle召唤童年:喜羊羊与灰太狼之懒羊羊绘画
开发语言·python
墨染风华不染尘5 小时前
python之开发笔记
开发语言·笔记·python
Dxy12393102165 小时前
python bmp图片转jpg
python
麦麦大数据5 小时前
Python棉花病虫害图谱系统CNN识别+AI问答知识neo4j vue+flask深度学习神经网络可视化
人工智能·python·深度学习