MacOS下Python开发环境的配置

系统默认Python2.7,需要安装Python3,并且可以随时切换。
使用Homebrew安装十分简单,一条命令搞定所有,看其他小伙伴有各种报错的,但是我没有遇到,不知道是不是最新的操作系统的原因?

 george@GeorgeMBP  ~  brew install python3
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
gatsby-cli                                                           netlify-cli
==> Updated Formulae
agda                       git-cinnabar               libuninameslist            sceptre                    wandio
ammonite-repl              glooctl                    lmod                       shc                        webpack
bgpstream                  guile                      mill                       sqlmap                     webtorrent-cli
calicoctl                  helmsman                   nco                        terragrunt                 xonsh
cfengine                   imageoptim-cli             odpi                       topgrade                   youtube-dl
collectd                   ios-webkit-debug-proxy     paket                      tox                        zshdb
convox                     janet                      picard-tools               unzip
enchant                    jenkins                    pmd                        userspace-rcu
ettercap                   juju                       protobuf-c                 uwsgi
ffsend                     libtrace                   re2                        vulkan-headers
==> Installing dependencies for python: gdbm, readline, sqlite and xz
==> Installing python dependency: gdbm
==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.18.1.mojave.bottle.1.tar.gz
######################################################################## 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: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.0_1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/fa/faab004773e6449dd97971311cb62a9bbaa44f1483b82640e818f0c355c8266d?__gda__=exp=156207841
######################################################################## 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.tar.gz
==> Downloading from https://akamai.bintray.com/e3/e360850758d2104b4ae9eab8ae57903a37b62c550c779ddf32100720f832508d?__gda__=exp=156207842
######################################################################## 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"' >> ~/.zshrc
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.gz
==> Downloading from https://akamai.bintray.com/01/010667293df282c8bceede3bcd36953dd57c56cef608d09a5b50694ab7d4b96b?__gda__=exp=156207847
######################################################################## 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.1.tar.gz
==> Downloading from https://akamai.bintray.com/cd/cd6b258f036893a4126975f4f5862664d2b46ae5acd9ba8747e2694f8c151e01?__gda__=exp=156207848
######################################################################## 100.0%
==> Pouring python-3.7.3.mojave.bottle.1.tar.gz
==> /usr/local/Cellar/python/3.7.3/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/py
==> /usr/local/Cellar/python/3.7.3/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/py
==> /usr/local/Cellar/python/3.7.3/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/py
==> 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
==> Summary
🍺  /usr/local/Cellar/python/3.7.3: 3,862 files, 59.9MB
==> Caveats
==> readline
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"
==> sqlite
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"' >> ~/.zshrc
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"
==> python
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
 george@GeorgeMBP  ~ 

配置双版本,首先在.bash_profile文件中添加如下配置:

# Setting PATH for Python 2.7
PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
# Setting PATH for Python 3.7.3
PATH="/usr/local/Cellar/python/3.7.3/bin:${PATH}"
export PATH

然后在.zshrc文件中加入别名:

alias python2='/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7'
alias python3='/usr/local/Cellar/python/3.7.3/bin/python3.7'
alias python=python3

PATH的配置放在.zshrc文件中也行,不过该文件内容较多,看着比较乱,不如放在.bash_profile看着清爽。当然为了.zshrc文件能够使用.bash_profile的配置,需要在.zshrc中加入一行:

source ~/.bash_profile

配置上述配置后,运行效果如下:

george@GeorgeMBP  ~  python
Python 3.7.3 (default, Jun 19 2019, 07:38:49)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
 george@GeorgeMBP  ~  python2
Python 2.7.10 (default, Feb 22 2019, 21:55:15)
[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.
>>>
 george@GeorgeMBP  ~  python3
Python 3.7.3 (default, Jun 19 2019, 07:38:49)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

你或许想:《去原作者写文章的地方

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
Python
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论