Mac中安装多个版本的python实战

Mac系统中默认已安装了一2.7版本python.

zhaohailin@zhaohailins-Mac-mini ~ % python

WARNING: Python 2.7 is not recommended. 
This version is included in macOS for compatibility with legacy software. 
Future versions of macOS will not include Python 2.7. 
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Jun  5 2020, 22:59:21) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello python")
hello python
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit();

问题:如何在mac中安装一个新的python版本,达到多个版本多版本python,正常使用呢?

1、使用homebrew安装和更新最近版本的python
zhaohailin@zhaohailins-Mac-mini ~ % brew upgrade python
Updating Homebrew...
Error: python not installed
zhaohailin@zhaohailins-Mac-mini ~ % brew install python
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles/gdbm-1.18.1_1.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles/readline-8.0.4.catalina.bottle.tar.gz
Already downloaded: /Users/zhaohailin/Library/Caches/Homebrew/downloads/881e3d38f51d9db95ce6b535e14af15a9361839b54362bd5d9cc3fb62de89fef--readline-8.0.4.catalina.bottle.tar.gz
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles/sqlite-3.33.0.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles/xz-5.2.5.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles/python%403.8-3.8.6.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/96e123a1befbb1a2f98577d728a52df126441de118e75edba0c1a3a1ca1380f3?res
######################################################################## 100.0%
==> Installing dependencies for python@3.8: gdbm, readline, sqlite and xz
==> Installing python@3.8 dependency: gdbm
==> Pouring gdbm-1.18.1_1.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/gdbm/1.18.1_1: 25 files, 641KB
==> Installing python@3.8 dependency: readline
==> Pouring readline-8.0.4.catalina.bottle.tar.gz
==> Caveats
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides BSD libedit.

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.4: 48 files, 1.5MB
==> Installing python@3.8 dependency: sqlite
==> Pouring sqlite-3.33.0.catalina.bottle.tar.gz
==> Caveats
sqlite is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

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.33.0: 11 files, 4MB
==> Installing python@3.8 dependency: xz
==> Pouring xz-5.2.5.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/xz/5.2.5: 92 files, 1.1MB
==> Installing python@3.8
==> Pouring python@3.8-3.8.6.catalina.bottle.tar.gz
==> /usr/local/Cellar/python@3.8/3.8.6/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/l
==> /usr/local/Cellar/python@3.8/3.8.6/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/l
==> /usr/local/Cellar/python@3.8/3.8.6/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/l
==> 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@3.8/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.8/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
🍺  /usr/local/Cellar/python@3.8/3.8.6: 4,341 files, 69.7MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/zhaohailin/Library/Logs/Homebrew/carthage... (64B)
Removing: /Users/zhaohailin/Library/Logs/Homebrew/brew-cask-completion... (116B)
Removing: /Users/zhaohailin/Library/Logs/Homebrew/jenv... (100B)
==> Caveats
==> readline
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides BSD libedit.

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 already provides this software and installing another version in
parallel can cause all kinds of trouble.

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@3.8
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@3.8/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.8/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
2、创建新安装python3软链接
zhaohailin@zhaohailins-Mac-mini ~ % ln -f /usr/local/bin/python3.8 /usr/local/bin/python
3、从新启动一个terminal窗口,然后运行python.此时就会发现运行的版本就会切换成刚刚最新安装的python3的版本
zhaohailin@zhaohailins-Mac-mini ~ % python
Python 3.8.6 (default, Oct  8 2020, 14:06:32) 
[Clang 12.0.0 (clang-1200.0.32.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit();
4、之所以使用python指令能够运行python.原因是我的环境变量中配置了/usr/local/bin
# HomeBrew
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/sbin:$PATH"
# HomeBrew END

检查发现环境变量中的确存在配置/usr/local/bin

$PATH
zsh: no such file or directory: /Users/zhaohailin/Library/Android/sdk/emulator:/Users/zhaohailin/Library/Android/sdk/build-tools/30.0.1:/Users/zhaohailin/Library/Android/sdk/platform-tools:/Users/zhaohailin/Library/Android/sdk/tools/bin:/Users/zhaohailin/Library/Android/sdk/tools:/Users/zhaohailin/.nvm/versions/node/v8.16.2/bin:/Users/zhaohailin/.jenv/shims:/Users/zhaohailin/.jenv/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/zhaohailin/Downloads/opt/maven/apache-maven-3.6.0/bin

总结:就这样愉快的入门python了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值