MacOS Big Sur 中的 NPM 错误 "Can't find Python executable"

NPM Error "Can't find Python executable" in MacOS Big Sur

我整整一周都在寻找这个问题的答案,但没有成功。我查看了每个 Whosebug post、Google 中的每篇文章以及我能找到的每个相关 Github 问题。大多数相关错误似乎都比较老,所以我想知道我的问题是否因为我使用的是 macOS Big Sur 而略有不同。

问题: 当我尝试在本地存储库中 运行 yarn install 时,我收到与 node-gyp 相关的错误和无法找到的 python 可执行文件。这是我的终端显示的内容:

yarn install v1.22.17

...other stuff

[4/4]   Building fresh packages...
[6/13] ⠐ node-sass
[2/13] ⠐ node-sass
[10/13] ⠐ metrohash
[4/13] ⠐ fsevents
error /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/metrohash: Command failed.
Exit code: 1
Command: node-gyp rebuild
Arguments:
Directory: /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/metrohash
Output:
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@12.18.0 | darwin | x64
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "/usr/local/opt/python@3.9/bin/python3", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/node-gyp/lib/configure.js:484:19)
gyp ERR! stack     at PythonFinder.<anonymous> (/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/node-gyp/lib/configure.js:406:16)
gyp ERR! stack     at F (/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/which/which.js:68:16)
gyp ERR! stack     at E (/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/which/which.js:80:29)
gyp ERR! stack     at /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/which/which.js:89:16
gyp ERR! stack     at /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/isexe/index.js:42:5
gyp ERR! stack     at /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/isexe/mode.js:8:5
gyp ERR! stack     at FSReqCallback.oncomplete (fs.js:167:21)
gyp ERR! System Darwin 20.6.0
gyp ERR! command "/Users/jimmiejackson/.nvm/versions/node/v12.18.0/bin/node" "/Users/jimmiejackson/Documents/repositories/repo-name/node_modules/metrohash/node_modules/.bin/node-gyp" "rebuild"
gyp ERR! cwd /Users/jimmiejackson/Documents/repositories/repo-name/node_modules/metrohash

我不完全确定此错误的含义或此节点模块为何搜索 python3。我试过 运行ning npm set config /path/to/python、下载 python3、在我的 .zshrc 配置文件中设置 PYTHON 路径,但似乎没有任何效果。完全有可能我对这个问题缺乏理解意味着我在正确的道路上,但没有 相当 得到正确的东西。有什么想法吗?

根据终端消息,您正在安装旧版本的 node-gyp (node-gyp@3.8.0)。通过快速搜索,该版本似乎需要 python 2。Python 2 应该出现在 Big Sur 中。正确设置路径,应该有效:

export PATH=/usr/bin/python:$PATH

另外,试试:

export PYTHON=/usr/bin/python

我相信您可以通过在其前面加上 npm_config:

来明确定义环境变量
$ export npm_config_python=/path/to/python

通过列出配置检查是否已配置:

$ npm config list
...

; environment configs
python = "/path/to/python"

这应该由 node-gyp 拿起。

另一种方法是在 .npmrc

中定义它
python = "/path/to/python"

第三种方法是全局设置:

npm config --global set python /path/to/python

阅读 gyp-node source 可能会有帮助。以下是您可以尝试的一些步骤。

  1. 安装python2。您应该确保在终端中,which -a python2 只有 returns 一个 python2python2 -V returns 正确的 2.x 版本。

  2. 覆盖 PYTHON 环境。 export PYTHON=python2.

  3. 重新运行安装。

如果仍然有错误,可能是错误信息不同。

这个问题也困扰了我一个星期,因为 node-gyp 实际上可以很好地找到我的 Python 实例,但是后来的构建步骤没有正确配置,所有流行的答案都没有'无法拿起 Python。我在 macOS Monterey (12.3.1) 上解决的步骤...

$ brew install pyenv

# Any modern version python should do. I don't think Python 2 is required any more.
$ pyenv install 3.10.3
$ pyenv global 3.10.3

# Add pyenv to your PATH so that you can reference python (not python3)
$ echo "export PATH=\"${HOME}/.pyenv/shims:${PATH}\"" >> ~/.zshrc

# open a new terminal window and confirm your pyenv version is mapped to python
$ which python
$ python --version

# Now try to re-run yarn install
$ yarn