AdSense

網頁

2021/8/15

Python Mac使用Homebrew安裝Python 3

在Mac使用Homebrew安裝Python 3的方式如下。


範例環境:

  • macOS Big Sur

macOS本身及附有Python 2,例如在終端機輸入python --version看看到版本為2.7.16,所以下面要用Homebrew安裝新的Python 3。

$ python --version
Python 2.7.16


Homebrew安裝Python 3

macOS當然要先裝好Homebrew,此為Mac的套件管理工具,可以用來下載及安裝Python 3。


Homebrew裝好後在終端機輸入brew intall python開始安裝Python 3(範例當時版本為3.9.0)。

$ brew install python
...
######################################################################## 100.0%
==> Installing dependencies for python@3.9: gdbm, mpdecimal, sqlite and xz
==> Installing python@3.9 dependency: gdbm
==> Pouring gdbm--1.20.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/gdbm/1.20: 24 files, 825.0KB
==> Installing python@3.9 dependency: mpdecimal
==> Pouring mpdecimal--2.5.1.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/mpdecimal/2.5.1: 71 files, 2.1MB
==> Installing python@3.9 dependency: sqlite
==> Pouring sqlite--3.36.0.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/sqlite/3.36.0: 11 files, 4.2MB
==> Installing python@3.9 dependency: xz
==> Pouring xz--5.2.5.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/xz/5.2.5: 95 files, 1.4MB
==> Installing python@3.9
==> Pouring python@3.9--3.9.6.big_sur.bottle.tar.gz
...
==> /usr/local/Cellar/python@3.9/3.9.6/bin/python3 -m ensurepip
==> /usr/local/Cellar/python@3.9/3.9.6/bin/python3 -m pip install -v --no-deps -
==> Caveats
Python has been installed as
  /usr/local/bin/python3
...
You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.9/site-packages

安裝結束後輸入python3 --version顯示Python 3的版本。

$ python3 --version
Python 3.9.0

Python 3開始會附帶pip套件管理工具,輸入pip3 --version顯示pip3版本。

$ pip3 --version
pip 20.2.3 from /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)

此時系統中有兩個Python,一個是macOS自帶的Python 2;另一則是剛Homebrew安裝的Python 3,操作時用python3命令即可。


沒有留言:

AdSense