macOS下安装python3

发布时间 2023-09-27 13:25:24作者: 可可西

使用brew安装python3

brew install python3

Running `brew update --auto-update`...
==> Downloading https://ghcr.io/v2/homebrew/portable-ruby/portable-ruby/blobs/sha256:61029cec31c68a1fae1fa90fa876adf43d0becff777da793f9b5c5577f00567a
##################################################################################################################################################################################### 100.0%
==> Pouring portable-ruby-2.6.10_1.el_capitan.bottle.tar.gz
==> Homebrew collects anonymous analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics have been recorded yet (nor will be during this `brew` run).

Installing from the API is now the default behaviour!
You can save space and time by running:
  brew untap homebrew/core
==> Downloading https://formulae.brew.sh/api/formula.jws.json
##################################################################################################################################################################################### 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.11/manifests/3.11.5
##################################################################################################################################################################################### 100.0%
==> Fetching dependencies for python@3.11: mpdecimal, ca-certificates, openssl@3, readline, sqlite and xz
==> Downloading https://ghcr.io/v2/homebrew/core/mpdecimal/manifests/2.5.1
##################################################################################################################################################################################### 100.0%
==> Fetching mpdecimal
==> Downloading https://ghcr.io/v2/homebrew/core/mpdecimal/blobs/sha256:91f795d74747bf8723022ac813f3f81d71fefb774116c27f936b2e32018a1926
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2023-08-22
##################################################################################################################################################################################### 100.0%
==> Fetching ca-certificates
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:a331e92e7a759571296581f029e5cc2ec7cee70cd92dc0b5f8eb76095f94a21a
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.1.3
##################################################################################################################################################################################### 100.0%
==> Fetching openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/blobs/sha256:2cc86941823ff633c21ef3533d7a106c4bddbcb35adac8fa6ef2d039c0860cdd
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/readline/manifests/8.2.1
##################################################################################################################################################################################### 100.0%
==> Fetching readline
==> Downloading https://ghcr.io/v2/homebrew/core/readline/blobs/sha256:abe9d3f3eec3ba2339860faa6a978b9909194c65c97a60b0d16f3d6d118879ea
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/sqlite/manifests/3.43.1-1
##################################################################################################################################################################################### 100.0%
==> Fetching sqlite
==> Downloading https://ghcr.io/v2/homebrew/core/sqlite/blobs/sha256:21a077eb227087d2e65580e34d306eef9086d17845d54df13db051a73d1e8e71
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/xz/manifests/5.4.4
##################################################################################################################################################################################### 100.0%
==> Fetching xz
==> Downloading https://ghcr.io/v2/homebrew/core/xz/blobs/sha256:4c25f68798c0b4c9b869e78fdfbd9cd7f8f723c51ea56d643b5644456288d69e
##################################################################################################################################################################################### 100.0%
==> Fetching python@3.11
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.11/blobs/sha256:c87f0729bff2c3ab0cb3a66f7187ff0c621eed150573506c88a9e51597c4e732
##################################################################################################################################################################################### 100.0%
==> Installing dependencies for python@3.11: mpdecimal, ca-certificates, openssl@3, readline, sqlite and xz
==> Installing python@3.11 dependency: mpdecimal
==> Downloading https://ghcr.io/v2/homebrew/core/mpdecimal/manifests/2.5.1
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/f367c2ee08c56b88be0662703a8e4275f8657608a268c8c44e845154b0cea543--mpdecimal-2.5.1.bottle_manifest.json
==> Pouring mpdecimal--2.5.1.ventura.bottle.tar.gz
?  /usr/local/Cellar/mpdecimal/2.5.1: 71 files, 2MB
==> Installing python@3.11 dependency: ca-certificates
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2023-08-22
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/a8cd32e30cae0b7335779e93a6554f294f09485802fc253a3a8be441337a6115--ca-certificates-2023-08-22.bottle_manifest.json
==> Pouring ca-certificates--2023-08-22.ventura.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
?  /usr/local/Cellar/ca-certificates/2023-08-22: 3 files, 221.7KB
==> Installing python@3.11 dependency: openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.1.3
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/f038f540886a6a94c000296efb3058dc47e867eef00f0c6836b8ff310bc04daa--openssl@3-3.1.3.bottle_manifest.json
==> Pouring openssl@3--3.1.3.ventura.bottle.tar.gz
?  /usr/local/Cellar/openssl@3/3.1.3: 6,495 files, 30.0MB
==> Installing python@3.11 dependency: readline
==> Downloading https://ghcr.io/v2/homebrew/core/readline/manifests/8.2.1
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/ab483c9a913ae82f3a2b3ae20918791bc3bd6825c7122a29cd4f1e0c65413759--readline-8.2.1.bottle_manifest.json
==> Pouring readline--8.2.1.ventura.bottle.tar.gz
?  /usr/local/Cellar/readline/8.2.1: 50 files, 1.7MB
==> Installing python@3.11 dependency: sqlite
==> Downloading https://ghcr.io/v2/homebrew/core/sqlite/manifests/3.43.1-1
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/658ed3d908f51e5e72235199d3c208ebdde607f88798d6e52d034029d8d90cd6--sqlite-3.43.1-1.bottle_manifest.json
==> Pouring sqlite--3.43.1.ventura.bottle.1.tar.gz
?  /usr/local/Cellar/sqlite/3.43.1: 11 files, 4.6MB
==> Installing python@3.11 dependency: xz
==> Downloading https://ghcr.io/v2/homebrew/core/xz/manifests/5.4.4
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/36353e8e9595891bff68a5cb421f60f3af5b1a68b6da82766c30dc3de86a8d3c--xz-5.4.4.bottle_manifest.json
==> Pouring xz--5.4.4.ventura.bottle.tar.gz
?  /usr/local/Cellar/xz/5.4.4: 163 files, 2.5MB
==> Installing python@3.11
==> Pouring python@3.11--3.11.5.ventura.bottle.tar.gz
==> /usr/local/Cellar/python@3.11/3.11.5/bin/python3.11 -Im ensurepip
==> /usr/local/Cellar/python@3.11/3.11.5/bin/python3.11 -Im pip install -v --no-index --upgrade --isolated --target=/usr/local/lib/python3.11/site-packages /usr/local/Cellar/python@3.11/3.
==> 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.11/libexec/bin

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

tkinter is no longer included with this formula, but it is available separately:
  brew install python-tk@3.11

gdbm (`dbm.gnu`) is no longer included in this formula, but it is available separately:
  brew install python-gdbm@3.11
`dbm.ndbm` changed database backends in Homebrew Python 3.11.
If you need to read a database from a previous Homebrew Python created via `dbm.ndbm`,
you'll need to read your database using the older version of Homebrew Python and convert to another format.
`dbm` still defaults to `dbm.gnu` when it is installed.

For more information about Homebrew and Python, see: https://docs.brew.sh/Homebrew-and-Python
==> Summary
?  /usr/local/Cellar/python@3.11/3.11.5: 3,287 files, 61MB
==> Running `brew cleanup python@3.11`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Upgrading 3 dependents of upgraded formulae:
Disable this behaviour by setting HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
openssl@1.1 1.1.1l_1 -> 1.1.1w, apr-util 1.6.1_3 -> 1.6.3_1, subversion 1.14.1_4 -> 1.14.2_3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/manifests/1.1.1w
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/apr-util/manifests/1.6.3_1
##################################################################################################################################################################################### 100.0%
==> Fetching dependencies for apr-util: apr
==> Downloading https://ghcr.io/v2/homebrew/core/apr/manifests/1.7.4
##################################################################################################################################################################################### 100.0%
==> Fetching apr
==> Downloading https://ghcr.io/v2/homebrew/core/apr/blobs/sha256:f12547e5dda5a279d9e179b177ba268a8f9d8bde75fd27e239d6a6c0b2badeba
##################################################################################################################################################################################### 100.0%
==> Fetching apr-util
==> Downloading https://ghcr.io/v2/homebrew/core/apr-util/blobs/sha256:127d4d4523d49a73e7dbf610f3e439ac2051a383edbf28cc18438faf78945ef0
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/subversion/manifests/1.14.2_3
##################################################################################################################################################################################### 100.0%
==> Fetching dependencies for subversion: gettext, lz4 and utf8proc
==> Downloading https://ghcr.io/v2/homebrew/core/gettext/manifests/0.22.2
##################################################################################################################################################################################### 100.0%
==> Fetching gettext
==> Downloading https://ghcr.io/v2/homebrew/core/gettext/blobs/sha256:b6bf2952286f24785b3e1bb6604f5d5c5e922e02d3e62aa3c92793a960421340
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/lz4/manifests/1.9.4
##################################################################################################################################################################################### 100.0%
==> Fetching lz4
==> Downloading https://ghcr.io/v2/homebrew/core/lz4/blobs/sha256:6a911ee2a3ea072f414d2983d532b28c34b63a68ff388a0008e1528dc0668838
##################################################################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/utf8proc/manifests/2.8.0
##################################################################################################################################################################################### 100.0%
==> Fetching utf8proc
==> Downloading https://ghcr.io/v2/homebrew/core/utf8proc/blobs/sha256:205b140d52a4e1c1fe8ff42514f95b230e7a9542789a7eb1da599b454aefd7df
##################################################################################################################################################################################### 100.0%
==> Fetching subversion
==> Downloading https://ghcr.io/v2/homebrew/core/subversion/blobs/sha256:8e15088554e65c305691a63e11a211eacd2baa3c85ccda4f2febc16c54f3d609
##################################################################################################################################################################################### 100.0%
==> Upgrading apr-util
  1.6.1_3 -> 1.6.3_1 

==> Installing dependencies for apr-util: apr
==> Installing apr-util dependency: apr
==> Downloading https://ghcr.io/v2/homebrew/core/apr/manifests/1.7.4
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/5cea0c08bd37febcd8cd169db1e67b7106cf42ff4165a1aadfbc2748d2a4058c--apr-1.7.4.bottle_manifest.json
==> Pouring apr--1.7.4.ventura.bottle.tar.gz
?  /usr/local/Cellar/apr/1.7.4: 59 files, 1.4MB
==> Installing apr-util
==> Pouring apr-util--1.6.3_1.ventura.bottle.tar.gz
?  /usr/local/Cellar/apr-util/1.6.3_1: 54 files, 871.2KB
==> Running `brew cleanup apr-util`...
Removing: /usr/local/Cellar/apr-util/1.6.1_3... (54 files, 878.5KB)
==> Upgrading subversion
  1.14.1_4 -> 1.14.2_3 

==> Installing dependencies for subversion: gettext, lz4 and utf8proc
==> Installing subversion dependency: gettext
==> Downloading https://ghcr.io/v2/homebrew/core/gettext/manifests/0.22.2
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/1d7107d862bee9bc06cd0edaeedde698981c5061b6de64c869aff2628e5e6953--gettext-0.22.2.bottle_manifest.json
==> Pouring gettext--0.22.2.ventura.bottle.tar.gz
?  /usr/local/Cellar/gettext/0.22.2: 2,040 files, 21.3MB
==> Installing subversion dependency: lz4
==> Downloading https://ghcr.io/v2/homebrew/core/lz4/manifests/1.9.4
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/379e59b981667f9585b33a2ff318769d8edca3ce6fd2e9a67ed291ae3e0cc872--lz4-1.9.4.bottle_manifest.json
==> Pouring lz4--1.9.4.ventura.bottle.tar.gz
?  /usr/local/Cellar/lz4/1.9.4: 22 files, 685.0KB
==> Installing subversion dependency: utf8proc
==> Downloading https://ghcr.io/v2/homebrew/core/utf8proc/manifests/2.8.0
Already downloaded: /Users/admin/Library/Caches/Homebrew/downloads/bbb5063945c794b7e1054a560bb8bf4bd7ba9c52e1c2a86474bff529691b1488--utf8proc-2.8.0.bottle_manifest.json
==> Pouring utf8proc--2.8.0.ventura.bottle.tar.gz
?  /usr/local/Cellar/utf8proc/2.8.0: 10 files, 747.4KB
==> Installing subversion
==> Pouring subversion--1.14.2_3.ventura.bottle.tar.gz
==> Caveats
svntools have been installed to:
  /usr/local/opt/subversion/libexec

The perl bindings are located in various subdirectories of:
  /usr/local/opt/subversion/lib/perl5
==> Summary
?  /usr/local/Cellar/subversion/1.14.2_3: 229 files, 29.5MB
==> Running `brew cleanup subversion`...
Removing: /usr/local/Cellar/subversion/1.14.1_4... (234 files, 32.2MB)
==> Checking for dependents of upgraded formulae...
==> No broken dependents found!
==> `brew cleanup` has not been run in the last 30 days, running now...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Removing: /usr/local/Cellar/apr/1.7.0_2... (104 files, 1.4MB)
Removing: /usr/local/Cellar/ca-certificates/2021-10-26... (3 files, 208.5KB)
Removing: /Users/admin/Library/Caches/Homebrew/cmake--3.24.2... (15.8MB)
Removing: /usr/local/Cellar/gettext/0.21... (1,953 files, 19.8MB)
Removing: /usr/local/Cellar/lz4/1.9.3... (22 files, 657.8KB)
Removing: /usr/local/Cellar/utf8proc/2.6.1... (10 files, 752.2KB)
Removing: /Users/admin/Library/Caches/Homebrew/portable-ruby-2.6.8_1.el_capitan.bottle.tar.gz... (8.8MB)
Removing: /Users/admin/Library/Caches/Homebrew/cmake_bottle_manifest--3.24.2... (10.4KB)
Removing: /Users/admin/Library/Logs/Homebrew/cmake... (64B)
Pruned 0 symbolic links and 2 directories from /usr/local
==> Caveats
==> python@3.11
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.11/libexec/bin

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

tkinter is no longer included with this formula, but it is available separately:
  brew install python-tk@3.11

gdbm (`dbm.gnu`) is no longer included in this formula, but it is available separately:
  brew install python-gdbm@3.11
`dbm.ndbm` changed database backends in Homebrew Python 3.11.
If you need to read a database from a previous Homebrew Python created via `dbm.ndbm`,
you'll need to read your database using the older version of Homebrew Python and convert to another format.
`dbm` still defaults to `dbm.gnu` when it is installed.

For more information about Homebrew and Python, see: https://docs.brew.sh/Homebrew-and-Python
==> subversion
svntools have been installed to:
  /usr/local/opt/subversion/libexec

The perl bindings are located in various subdirectories of:
  /usr/local/opt/subversion/lib/perl5

 

查看机器上python3和pip3的位置

where python3

/usr/local/bin/python3   // 安装后的python3版本
/usr/bin/python3  // Xcode command line developer tools自带的

where pip3

/usr/local/bin/pip3  // 安装后的pip3版本
/usr/bin/pip3   //  Xcode command line developer tools自带的

ls -li /usr/local/bin/python3

409888026 lrwxr-xr-x  1 admin  admin  40  9 26 10:06 /usr/local/bin/python3 -> ../Cellar/python@3.11/3.11.5/bin/python3  // python实际在/usr/local/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/Resources/Python.app/Contents/MacOS/Python

ls -li /usr/local/bin/pip3

409892281 lrwxr-xr-x  1 admin  admin  37  9 26 10:06 /usr/local/bin/pip3 -> ../Cellar/python@3.11/3.11.5/bin/pip3

 

配置shell缺省执行的python3和pip3的环境变量和别名

向~/.zshrc中写入脚本    注:macOS的缺省的shell为zsh

echo "export PATH=/usr/local/bin/python3:${PATH}" >> ~/.zshrc  // 将/usr/local/bin/python3添加到PATH环境变量的头部
echo "alias python3=/usr/local/bin/python3" >> ~/.zshrc   // 将python3别名设置为/usr/local/bin/python3
echo "alias pip3=/usr/local/bin/pip3" >> ~/.zshrc   // 将pip3别名设置为/usr/local/bin/pip3

cat ~/.zshrc查看.zshrc中的内容为:

export PATH=/usr/local/bin/python3:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
alias python3=/usr/local/bin/python3
alias pip3=/usr/local/bin/pip3

在当前shell中,让~/.zshrc立即生效 

source ~/.zshrc

注1:source命令会在当前bash环境下读取并执行scripts文件中的命令。

注2:不加source直接执行scripts文件会启动一个子shell来执行命令。这样如果把设置环境变量(或alias等)的命令写进scripts文件中,就只会影响子shell,无法改变当前的bash。