macos 安装 python3 环境

本文介绍了如何通过Homebrew在MacOS系统中安装Python3.7,包括搜索可用的Python版本,选择安装3.7而非3.8的原因,安装过程,以及安装后的配置步骤,如修改PATH变量,确保系统默认使用Python3.7,并展示了如何验证安装成功。
摘要由CSDN通过智能技术生成

Brew 安装

  1. 搜索可以安装的包
brew search python

==> Formulae
app-engine-python                 bpython                           micropython                       python-markdown                   python-yq                         python@3.9 ✔                      pythran
boost-python                      gst-python                        ptpython                          python-tabulate                   python@3.7                        reorder-python-imports            jython
boost-python3                     ipython                           python-launcher                   python-tk@3.9                     python@3.8 ✔                      wxpython                          cython
==> Casks
homebrew/cask/awips-python                                                                                             homebrew/cask/mysql-connector-python
  1. 安装
# 这里我安装了3.7, 没有选择3.8, 担心有些组件包对3.8支持不好
brew install python@3.7

==> Caveats
Python has been installed as
  /usr/local/opt/python@3.7/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.7/libexec/bin

You can install Python packages with
  /usr/local/opt/python@3.7/bin/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

python@3.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have python@3.7 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/python@3.7/bin:$PATH"' >> ~/.zshrc

For compilers to find python@3.7 you may need to set:
  export LDFLAGS="-L/usr/local/opt/python@3.7/lib"

For pkg-config to find python@3.7 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/python@3.7/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/python@3.7/3.7.11: 4,619 files, 71.6MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /usr/local/Cellar/apr/1.7.0... (59 files, 1.4MB)
Removing: /usr/local/Cellar/autoconf/2.69... (70 files, 3.0MB)
Removing: /usr/local/Cellar/c-ares/1.16.0... (74 files, 530.3KB)
Removing: /usr/local/Cellar/curl/7.69.1... (457 files, 3.4MB)
Removing: /usr/local/Cellar/freetds/1.1.32... (66 files, 3.6MB)
Removing: /usr/local/Cellar/freetype/2.10.1... (61 files, 2.2MB)
Removing: /usr/local/Cellar/gdbm/1.18.1... (20 files, 602.8KB)
Removing: /usr/local/Cellar/gmp/6.2.0... (20 files, 3.2MB)
Removing: /usr/local/Cellar/icu4c/64.2... (257 files, 69.3MB)
Removing: /usr/local/Cellar/krb5/1.18... (162 files, 4MB)
Removing: /Users/haolin/Library/Caches/Homebrew/libevent--2.1.12... (681.3KB)
Removing: /usr/local/Cellar/libffi/3.2.1... (16 files, 296.9KB)
Removing: /usr/local/Cellar/libidn2/2.0.5... (68 files, 668.8KB)
Removing: /usr/local/Cellar/libpq/12.2_1... (2,240 files, 23.8MB)
Removing: /usr/local/Cellar/libtiff/4.1.0... (247 files, 3.7MB)
Removing: /usr/local/Cellar/libtool/2.4.6_1... (70 files, 3.7MB)
Removing: /usr/local/Cellar/libzip/1.6.1... (135 files, 598.5KB)
Removing: /usr/local/Cellar/m4/1.4.18... (13 files, 621.6KB)
Removing: /Users/haolin/Library/Caches/Homebrew/ncurses--6.2... (2.3MB)
Removing: /usr/local/Cellar/nghttp2/1.40.0... (26 files, 2.7MB)
Removing: /usr/local/Cellar/openldap/2.4.49... (328 files, 6.7MB)
Removing: /Users/haolin/Library/Caches/Homebrew/openssl@1.1--1.1.1k... (5.4MB)
Removing: /usr/local/Cellar/readline/8.0.4... (48 files, 1.5MB)
Removing: /usr/local/Cellar/sqlite/3.31.1... (11 files, 4MB)
Removing: /usr/local/Cellar/tidy-html5/5.6.0... (14 files, 2.6MB)
Removing: /Users/haolin/Library/Caches/Homebrew/tmux--3.2a... (430.3KB)
Removing: /usr/local/Cellar/unixodbc/2.3.7... (46 files, 1.9MB)
Removing: /Users/haolin/Library/Caches/Homebrew/utf8proc--2.6.1... (173KB)
Removing: /usr/local/Cellar/webp/1.1.0... (39 files, 2.1MB)
Removing: /Users/haolin/Library/Caches/Homebrew/openssl@1.1_bottle_manifest--1.1.1k... (6KB)
Removing: /Users/haolin/Library/Caches/Homebrew/portable-ruby-2.6.3_2.yosemite.bottle.tar.gz... (9.1MB)
Removing: /Users/haolin/Library/Caches/Homebrew/tmux_bottle_manifest--3.2a... (6.8KB)
Removing: /Users/haolin/Library/Caches/Homebrew/ncurses_bottle_manifest--6.2... (7.3KB)
Removing: /Users/haolin/Library/Caches/Homebrew/libevent_bottle_manifest--2.1.12... (6.9KB)
Removing: /Users/haolin/Library/Caches/Homebrew/utf8proc_bottle_manifest--2.6.1... (5.3KB)
Pruned 1 symbolic links and 8 directories from /usr/local

总结下
安装目录: /usr/local/opt/python@3.7/bin/python3
命令工具目录: /usr/local/opt/python@3.7/libexec/bin
包安装命令: /usr/local/opt/python@3.7/bin/pip3 install
包安装目录: /usr/local/lib/python3.7/site-packages

  1. 链接 python3
    因为 Macos 系统默认自带的是 python 2.7, 需要将修改系统 PATH 变量, 改为默认使用 python3
# 导入 python3 命令路径
echo 'export PATH="/usr/local/opt/python@3.7/bin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/usr/local/opt/python@3.7/lib"
export PKG_CONFIG_PATH="/usr/local/opt/python@3.7/lib/pkgconfig"
# 加载配置
source ~/.zshrc
# 测试
python3 --version
Python 3.7.11
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值