【问题标题】:NPM Error "Can't find Python executable" in MacOS Big SurMacOS Big Sur 中的 NPM 错误“找不到 Python 可执行文件”
【发布时间】:2022-04-22 00:28:44
【问题描述】:

我一直在寻找这个问题的答案整整一周,但没有成功。我查看了每一篇 StackOverflow 帖子、每篇来自 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。我试过运行npm set config /path/to/python,下载python3,在我的.zshrc 配置文件中设置PYTHON 路径,但似乎没有任何效果。完全有可能我对这个问题缺乏了解,这意味着我走在正确的道路上,但没有完全得到正确的结果。有什么想法吗?

【问题讨论】:

  • 你是说export PYTHON=/usr/bin/python3 没有解决这个问题?
  • @TimRoberts 我假设您的意思是将其添加到我的 .zshrc 中?我的 zsh 中确实有它,但它仍然无法正常工作。
  • 您应该能够在运行yarn 命令之前在命令行中键入它。如果它飞了,那么这会给你一个线索。
  • @TimRoberts 我明白了。我在终端中运行了它,不幸的是,yarn install 产生了同样的错误。
  • /usr/local/opt 中的 python 由 homebrew 管理。您可以尝试至少暂时从您的PATH 中删除/usr/local/opt 吗?或者,您可以像这样将/usr/bin 添加到您的路径中:PATH=/usr/bin:$PATH yarn install ...

标签: python macos npm node-gyp


【解决方案1】:

这个问题也困扰了我一周,因为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

【讨论】:

    【解决方案2】:

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

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

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

    3. 重新运行安装。

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

    【讨论】:

    • 这似乎已经解决了!我知道我一直很接近,但你的第 2 步似乎是我没有尝试过的。谢谢!
    • 是的,我在阅读代码时有点惊讶,PYTHON env 无论如何都会附加PATH,所以将 python 的完整路径放在那里是错误的。这有点奇怪,很容易解释你的问题,但可以肯定的是,这可能只是因为你的项目中的一个依赖项使用了旧的node-gyp
    【解决方案3】:

    我相信您可以通过在 env var 前面加上 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
    

    【讨论】:

      【解决方案4】:

      从终端消息中,您正在安装旧版本的 node-gyp (node-gyp@3.8.0)。通过快速搜索,可以看出此版本需要 python 2。Big Sur 中应该存在 Python 2。正确设置路径,应该可以:

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

      另外,试试:

      export PYTHON=/usr/bin/python
      

      【讨论】:

        猜你喜欢
        • 2021-05-04
        • 2021-03-31
        • 2022-11-09
        • 2021-02-27
        • 1970-01-01
        • 2021-11-18
        • 2022-01-09
        • 2017-07-03
        • 1970-01-01
        相关资源
        最近更新 更多