mac下安装Python3.*(最新版本)

前言: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.*版本。知乎上已经贴出了各类各样的方法,我这里就很少作赘述了。

转载请注明出处http://www.cnblogs.com/meng1314-shuai/p/9031686.html