说明
- Anaconda指的是一个开源的Python发行版本,包括了python和很多常见的软件库, 和一个包管理器conda。常见的科学计算类的库都包含在里面了,使得安装比常规python安装要容易。
- Anaconda是专注于数据分析的Python发行版本,包含了conda、Python等190多个科学包及其依赖项。
第一步,安装Homebrew
安装方法参考:juejin.cn/post/719808…
Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。 简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。
第二步,安装Anaconda
执行命令brew search anaconda
~:brew search anaconda
==> Casks
anaconda pycharm-ce-with-anaconda-plugin pycharm-with-anaconda-plugin
再执行brew install anaconda
进行安装
~: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
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
,写好后的效果如下:
export ANACONDA="/usr/local/anaconda3/bin"
# 写入环境变量
export PATH=$ANACONDA:$RUBY:$GEMS:$JAVA_HOME/bin:$FLUTTER/bin:$PATH
3. 执行source ~/.zshrc
指令让系统重新加载环境配置文件
4. 执行conda info
指令,显示conda信息则表示安装和配置没问题
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环境
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
指令进行创建:
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
可以看到我们新建的环境了:
conda env list
# conda environments:
#
base /usr/local/anaconda3
myenv /usr/local/anaconda3/envs/myenv
来到myenv
环境的路径下可以看到确实是python3.8版本:
第五步,VSCode + Anaconda搭建Python环境
重启VSCode打开一个项目,点击右下角的python版本就能看到刚才创建的anaconda环境,点击某个环境即可切换
原文链接:https://juejin.cn/post/7346763303430094857 作者:给我一个抱枕