最近项目上需编译Node老项目,用到Python2。可Mac Monterey默认移除了Python2。实际上Python官方宣布 2020 年 1 月后不再更新维护 Python2,最后版本为2.7.18。
在2020年4月之前可使用 brew install python@2
来安装python2,但实测已失效,报错如下:
❯ brew install python@2
Warning: No available formula with the name "python@2". Did you mean ipython, bpython, jython or cython?
==> Searching for similarly named formulae...
These similarly named formulae were found:
ipython bpython jython cython
To install one of them, run (for example):
brew install ipython
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching taps on GitHub...
Error: No formulae found in taps.
Copybrew install python@2
随后找到 python@2.rb
,同样失效,报错如下:
❯ wget https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb
--2022-04-20 12:42:31-- https://raw.githubusercontent.com/Homebrew/homebrew-core/86a44a0a552c673a05f11018459c9f5faae3becc/Formula/python@2.rb
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.111.133, 185.199.110.133, ...
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:13347 (13K) [text/plain]
正在保存至: “python@2.rb”
python@2.rb 100%[=======================================================================================================>] 13.03K 82.1KB/s 用时 0.2s
2022-04-20 12:42:33 (82.1 KB/s) - 已保存 “python@2.rb” [13347/13347])
❯ brew install python@2.rb
Error: python@2: Invalid bottle tag symbol
Copypython@2.rb
功夫不负有心人,直到找到 pyenv
,原文见下:
https://opensource.com/article/19/5/python-3-default-mac
只需3步,轻松在Mac上管理Python版本。
- 执行
brew install pyenv
:
❯ brew install pyenv
==> Downloading https://ghcr.io/v2/homebrew/core/m4/manifests/1.4.19
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/m4/blobs/sha256:8a17c921e5135206c382fc67ae53ba8835684dac5bfe7eb2bcdfa79df4d2731d
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:8a17c921e5135206c382fc67ae53ba8835684dac5bfe7eb2bcdfa79df4d2731d?se=2022-04-20T04%3A50%3A00Z&sig=yW%2Bt50Q2W
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/autoconf/manifests/2.71
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/autoconf/blobs/sha256:de8f4aa4123d307ad8bb11b1c685538224dc39939085fd00af928c5099c4202d
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:de8f4aa4123d307ad8bb11b1c685538224dc39939085fd00af928c5099c4202d?se=2022-04-20T04%3A50%3A00Z&sig=PPvxHCgSGE%
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pkg-config/manifests/0.29.2_3
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pkg-config/blobs/sha256:a6ba80711f98b65d8a2bf2c9278540860415e9b5e545da338a4d94f39d119285
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:a6ba80711f98b65d8a2bf2c9278540860415e9b5e545da338a4d94f39d119285?se=2022-04-20T04%3A50%3A00Z&sig=542WNS%2Bad
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pyenv/manifests/2.2.5
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pyenv/blobs/sha256:b30cfd4badab10b271f800997097737ca9524409d91a6c646af033864c4ea5fe
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:b30cfd4badab10b271f800997097737ca9524409d91a6c646af033864c4ea5fe?se=2022-04-20T04%3A55%3A00Z&sig=481TVjOSfV1
######################################################################## 100.0%
==> Installing dependencies for pyenv: m4, autoconf and pkg-config
==> Installing pyenv dependency: m4
==> Pouring m4--1.4.19.monterey.bottle.tar.gz
? /usr/local/Cellar/m4/1.4.19: 13 files, 740.8KB
==> Installing pyenv dependency: autoconf
==> Pouring autoconf--2.71.monterey.bottle.tar.gz
? /usr/local/Cellar/autoconf/2.71: 71 files, 3.2MB
==> Installing pyenv dependency: pkg-config
==> Pouring pkg-config--0.29.2_3.monterey.bottle.tar.gz
? /usr/local/Cellar/pkg-config/0.29.2_3: 11 files, 657KB
==> Installing pyenv
==> Pouring pyenv--2.2.5.monterey.bottle.tar.gz
? /usr/local/Cellar/pyenv/2.2.5: 877 files, 2.9MB
==> Running `brew cleanup pyenv`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Copy
2. 第二步安装python,pyenv install 2.7.18
:
❯ pyenv install 2.7.18
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-2.7.18.tar.xz...
-> https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz
Installing Python-2.7.18...
patching file configure
patching file configure.ac
patching file setup.py
patching file Mac/Tools/pythonw.c
patching file setup.py
patching file Doc/library/ctypes.rst
patching file Lib/test/test_str.py
patching file Lib/test/test_unicode.py
patching file Modules/_ctypes/_ctypes.c
patching file Modules/_ctypes/callproc.c
patching file Modules/_ctypes/ctypes.h
patching file Modules/_ctypes/callproc.c
patching file setup.py
patching file Mac/Modules/qt/setup.py
patching file setup.py
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-2.7.18 to /Users/jerry/.pyenv/versions/2.7.18
Copy
3. 设置全局默认 pyenv global 2.7.18
❯ pyenv global 2.7.18
❯ pyenv global
2.7.18
❯ pyenv version
2.7.18 (set by /Users/jerry/.pyenv/version)
可写入 .zshrc
或 .bash_profile
中
❯ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
❯ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Copy
下面需要在 npm install
的时候,使用python2进行编译:
❯ npm config set python /Users/jerry/.pyenv/versions/2.7.18/bin/python
Copy
大功告成!