前言:mac系統自帶python,不過以當前mac系統的最新版本為例,自帶的python版本都是2.版本,雖然不影響老版本項目的運行,但是python最新的3.版本的一些語法與2.*版本並不相同,網上的教程大神們也肯定都更新出了最新版的教程,我們不論是學習還是使用,當然用最新版會更好一點。
1、在安裝最新版Python3.*之前,我們先熟悉一下系統自帶的python。
Mac系統自帶python路徑為/System/Library/Frameworks/Python.framework/Version,我們先來打開目錄看一下:
open /System/Library/Frameworks/Python.framework/Versions

我們看到這里有多個python版本,而在Current目錄下存放的是系統當前的python版本。
mac既然自帶了python,當然肯定配置好了python的全局命令,我們直接在終端運行:
python
Python 2.7.10 (default, Feb 22 2019, 21:17:52)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
運行正常。到這里也差不多對mac系統自帶的python有所了解,接下來我們開始安裝最新版本的python。
2、開始安裝(這里我們使用神器homebrew,還不熟悉此神器的小伙伴請自行百度,也可點擊鏈接直接前往下載)
安裝前先搜索一下是否已經存在python3的包:
brew search python3

已經存在,我們可以直接安裝了:
brew install python3
Updating Homebrew...
==> Installing dependencies for python: gdbm****, openssl****, readline****, sqlite and xz
==> Installing python dependency: gdbm
==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.18.1.mojave.bottle.1
######################################################################## 100.0%
==> Pouring gdbm-1.18.1.mojave.bottle.1.tar.gz
🍺 /usr/local/Cellar/gdbm/1.18.1: 20 files, 586.8KB
==> Installing python dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2r.mojave.bottl
==> Downloading from https://akamai.bintray.com/c1/c1f8c06740398325c7028213b20b1
######################################################################## 100.0%
==> Pouring openssl-1.0.2r.mojave.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
/usr/local/etc/openssl/certs
and run
/usr/local/opt/openssl/bin/c_rehash
openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.
If you need to have openssl first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
For compilers to find openssl you may need to set:
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
==> Summary
🍺 /usr/local/Cellar/openssl/1.0.2r: 1,795 files, 12.1MB
==> Installing python dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.0_1.mojave.bot
==> Downloading from https://akamai.bintray.com/fa/faab004773e6449dd97971311cb62
######################################################################## 100.0%
==> Pouring readline-8.0.0_1.mojave.bottle.tar.gz
==> Caveats
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.
For compilers to find readline you may need to set:
export LDFLAGS="-L/usr/local/opt/readline/lib"
export CPPFLAGS="-I/usr/local/opt/readline/include"
==> Summary
🍺 /usr/local/Cellar/readline/8.0.0_1: 48 files, 1.5MB
==> Installing python dependency: sqlite
==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.28.0.mojave.bottle
==> Downloading from https://akamai.bintray.com/e3/e360850758d2104b4ae9eab8ae579
######################################################################## 100.0%
==> Pouring sqlite-3.28.0.mojave.bottle.tar.gz
==> Caveats
sqlite is keg-only, which means it was not symlinked into /usr/local,
because macOS provides an older sqlite3.
If you need to have sqlite first in your PATH run:
echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.bash_profile
For compilers to find sqlite you may need to set:
export LDFLAGS="-L/usr/local/opt/sqlite/lib"
export CPPFLAGS="-I/usr/local/opt/sqlite/include"
==> Summary
🍺 /usr/local/Cellar/sqlite/3.28.0: 11 files, 3.7MB
==> Installing python dependency: xz
==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.4.mojave.bottle.tar.
==> Downloading from https://akamai.bintray.com/01/010667293df282c8bceede3bcd369
######################################################################## 100.0%
==> Pouring xz-5.2.4.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/xz/5.2.4: 92 files, 1MB
==> Installing python
==> Downloading https://homebrew.bintray.com/bottles/python-3.7.3.mojave.bottle.
==> Downloading from https://akamai.bintray.com/25/25e0099852136c4ef1efd221247d0
######################################################################## 100.0%
==> Pouring python-3.7.3.mojave.bottle.tar.gz
Error: An unexpected error occurred during the brew link step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/Frameworks
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworksf
所有的包都下載完畢,但是我們卻發現最后報了兩條錯誤,大概意思是公式已經建立,但是在創建連接的時候因為沒有權限而發生意外錯誤。在報錯信息里我們看到一個目錄/usr/local/Frameworks,我們先去找一下這個目錄:
open /usr/local/Frameworks
發現這個目錄竟然不存在,那我們就來手動創建一個:
sudo mkdir /usr/local/Frameworks
創建好目錄之后,再來解決權限問題:
sudo chown $(whoami):admin /usr/local/Frameworks
不報錯即為修改成功,修改好權限之后,還需要手動執行一下安裝時未完成的創建連接:
brew link python3
Linking /usr/local/Cellar/python/3.7.3... 20 symlinks created
連接成功。咱們來查看一下當前系統下的python3的信息:
brew info python3
python: stable 3.7.3 (bottled), HEAD
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python/3.7.3 (3,209 files, 51MB) *
Poured from bottle on 2019-05-07 at 17:43:32
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/python.rb
==> Dependencies
Build: pkg-config ✘
Required: gdbm ✔, openssl ✔, readline ✔, sqlite ✔, xz ✔
==> Options
--HEAD
Install HEAD version
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
If you need Homebrew's Python 2.7 run
brew install python@2
You can install Python packages with
pip3 install <package>
They will install into the site-package directory
/usr/local/lib/python3.7/site-packages
See: https://docs.brew.sh/Homebrew-and-Python
==> Analytics
install: 431,182 (30 days), 1,427,668 (90 days), 4,086,118 (365 days)
install_on_request: 211,675 (30 days), 728,718 (90 days), 2,390,316 (365 days)
build_error: 0 (30 days)
發現python3被安裝到了/usr/local/bin/python3目錄下,有興趣的小伙伴自行前往查看想過配置文件。
到這里python3的安裝就算完成了,不過可能有小伙伴已經發現,不管是在終端運行python -V查看版本號還是直接運行python啟動python命令行模式,默認的python版本還是系統自帶的2.版本。其實這時候只運行命令時需要把python改為python3就行了,當然,有興趣的小伙伴也可以嘗試配置一下默認版本,把當前系統的默認版本修改為3.版本。知乎上已經貼出了各種各樣的方法,我這里就不多做贅述了。
