前言:mac系統自帶python,不過以當前mac系統的最新版本爲例,自帶的python版本都是2.*版本,雖然不影響老版本項目的運行,可是python最新的3.*版本的一些語法與2.*版本並不相同,網上的教程大神們也確定都更新出了最新版的教程,咱們不管是學習仍是使用,固然用最新版會更好一點。html
轉載請註明出處http://www.cnblogs.com/meng1314-shuai/p/9031686.htmlpython
一、在安裝最新版Python3.*以前,咱們先熟悉一下系統自帶的python。sql
Mac系統自帶python路徑爲/System/Library/Frameworks/Python.framework/Version,咱們先來打開目錄看一下:
bootstrap
open /System/Library/Frameworks/Python.framework/Versions
咱們看到這裏有多個python版本,而在Current目錄下存放的是系統當前的python版本。xcode
mac既然自帶了python,固然確定配置好了python的全局命令,咱們直接在終端運行:bash
python
運行正常。到這裏也差很少對mac系統自帶的python有所瞭解,接下來咱們開始安裝最新版本的python。app
二、開始安裝(這裏咱們使用神器homebrew,還不熟悉此神器的小夥伴請自行百度,也可點擊連接直接前往下載)ide
安裝前先搜索一下是否已經存在python3的包:post
brew search python3
已經存在,咱們能夠直接安裝了:學習
brew install python3
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
angular-cli fluent-bit links postgresql@9.6 texmath
byacc fn mlt qcachegrind tile38
caddy ipython osrm-backend rhino wtf
cfitsio ipython@5 postgresql siril
cfssl libpq postgresql@9.4 sundials
dnscrypt-proxy libpqxx postgresql@9.5 synfig
==> Installing dependencies for python: sphinx-doc, gdbm, openssl, sqlite, xz
==> Installing python dependency: sphinx-doc
==> Downloading https://homebrew.bintray.com/bottles/sphinx-doc-1.7.4.high_sierr
######################################################################## 100.0%
==> Pouring sphinx-doc-1.7.4.high_sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because this formula is mainly used internally by other formulae.
Users are advised to use `pip` to install sphinx-doc.
If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/sphinx-doc/bin:$PATH"' >> ~/.bash_profile
==> Summary
🍺 /usr/local/Cellar/sphinx-doc/1.7.4: 2,642 files, 36.8MB
==> Installing python dependency: gdbm
==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.14.1_1.high_sierra.b
######################################################################## 100.0%
==> Pouring gdbm-1.14.1_1.high_sierra.bottle.tar.gz
🍺 /usr/local/Cellar/gdbm/1.14.1_1: 20 files, 555.7KB
==> Installing python dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o_1.high_sierr
######################################################################## 100.0%
==> Pouring openssl-1.0.2o_1.high_sierra.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
This formula 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 this software first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
==> Summary
🍺 /usr/local/Cellar/openssl/1.0.2o_1: 1,791 files, 12.3MB
==> Installing python dependency: sqlite
==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.23.1.high_sierra.b
######################################################################## 100.0%
==> Pouring sqlite-3.23.1.high_sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS provides an older sqlite3.
If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.bash_profile
For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/sqlite/lib
CPPFLAGS: -I/usr/local/opt/sqlite/include
For pkg-config to find this software you may need to set:
PKG_CONFIG_PATH: /usr/local/opt/sqlite/lib/pkgconfig
==> Summary
🍺 /usr/local/Cellar/sqlite/3.23.1: 11 files, 3MB
==> Installing python dependency: xz
==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.4.high_sierra.bottle
######################################################################## 100.0%
==> Pouring xz-5.2.4.high_sierra.bottle.tar.gz
🍺 /usr/local/Cellar/xz/5.2.4: 92 files, 1MB
==> Installing python
Warning: Building python from source:
The bottle needs the Apple Command Line Tools to be installed.
You can install them, if desired, with:
xcode-select --install
==> Downloading https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/python/3.6.5 --enable-ipv6 --dataroot
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python/3.6.5
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python/3.6.5/sha
==> Downloading https://files.pythonhosted.org/packages/72/c2/c09362ab29338413ab
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/ae/e8/2340d46ecadb1692a1
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/5d/c1/45947333669b31bc6b
######################################################################## 100.0%
==> make html
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/Frameworks
全部的包都下載完畢,可是咱們卻發現最後報了兩條錯誤,大概意思是公式已經創建,可是在建立鏈接的時候由於沒有權限而發生意外錯誤。在報錯信息裏咱們看到一個目錄/usr/local/Frameworks,咱們先去找一下這個目錄:
open /usr/local/Frameworks
發現這個目錄居然不存在,那咱們就來手動建立一個:
sudo mkdir /usr/local/Frameworks
建立好目錄以後,再來解決權限問題:
sudo chown $(whoami):admin /usr/local/Frameworks
不報錯即爲修改爲功,修改好權限以後,還須要手動執行一下安裝時未完成的建立鏈接:
brew link python3
鏈接成功。我們來查看一下當前系統下的python3的信息:
brew info python3
發現python3被安裝到了/usr/local/bin/python3目錄下,有興趣的小夥伴自行前往查看想過配置文件。
到這裏python3的安裝就算完成了,不過可能有小夥伴已經發現,無論是在終端運行python -V查看版本號仍是直接運行python啓動python命令行模式,默認的python版本仍是系統自帶的2.*版本。其實這時候只運行命令時須要把python改成python3就好了,固然,有興趣的小夥伴也能夠嘗試配置一下默認版本,把當前系統的默認版本修改成3.*版本。知乎上已經貼出了各類各樣的方法,我這裏就很少作贅述了。