簡體   English   中英

在 MacOS Big Sur 中出現 NPM 錯誤“找不到 Python 可執行文件”

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

我一直在尋找這個問題的答案整整一周,但沒有成功。 我查看了每個 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,在 my.zshrc 配置文件中設置 PYTHON 路徑,但似乎沒有任何效果。 完全有可能我對這個問題缺乏了解意味着我走在正確的道路上,但沒有完全正確。 有任何想法嗎?

這個問題也困擾了我一個星期,因為node-gyp 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

閱讀gyp-node 源代碼可能會有所幫助。 以下是您可以嘗試的一些步驟。

  1. 安裝python2。 您應該確保在終端中, which -a python2只返回一個python2並且python2 -V返回正確的 2.x 版本。

  2. 覆蓋PYTHON export PYTHON=python2

  3. 重新運行安裝。

如果仍然有錯誤,則錯誤消息可能不同。

我相信您可以通過在npm_config前面加上前綴來明確定義 env var:

$ 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

如果您像我一樣從節點 14 升級到節點 16,您可能會看到這個問題。 在這種情況下,一個簡單的解決方法可能是確保 yarn 將node-sass解析為版本 6。

在你的package.json中設置這個:

 "resolutions": {
    "node-sass": "^6.0.1"
  }

從終端消息中,您正在安裝舊版本的 node-gyp (node-gyp@3.8.0)。 通過快速搜索,可以看出此版本需要 python 2。 Python 2 應該存在於Big Sur中。 正確設置路徑,應該可以工作:

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

另外,嘗試:

export PYTHON=/usr/bin/python

MacOS Monterrey 已移除對 python2 的支持。 我已經將它指向 python 3 但是有一個沒有括號的打印語句,所以代碼總是會失敗! 任何如何讓這個工作的想法我都無法安裝 npm 來獲取依賴項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM