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

前言: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

location_python2

我们看到这里有多个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

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.版本。知乎上已经贴出了各种各样的方法,我这里就不多做赘述了。

站在巨人的肩膀上摘苹果:

原文:http://www.cnblogs.com/meng1314-shuai/p/9031686.html

posted @ 2020-02-06 19:16  未月廿三  阅读(6476)  评论(0编辑  收藏  举报