简体   繁体   English

在 MacOS Big Sur 中出现 NPM 错误“找不到 Python 可执行文件”

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

I've been looking for the answer to this for a good solid week now, with no success.我一直在寻找这个问题的答案整整一周,但没有成功。 I've looked at every StackOverflow post, every article from Google and every related Github issue I could find.我查看了每个 StackOverflow 帖子、Google 的每篇文章以及我能找到的每个相关的 Github 问题。 Most related errors seem to be older, so I'm wondering if my issue is slightly different due to me being on macOS Big Sur.大多数相关错误似乎都比较老,所以我想知道我的问题是否由于我在 macOS Big Sur 上而略有不同。

The issue: When I try to run yarn install in my local repo, I receive an error related to node-gyp and a python executable that is unable to be found.问题:当我尝试在本地存储库中运行yarn install时,我收到与 node-gyp 相关的错误和无法找到的 python 可执行文件。 Here is what my terminal shows:这是我的终端显示的内容:

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

I'm not entirely sure what this error means or why this node module is searching for python3.我不完全确定这个错误是什么意思,或者为什么这个节点模块正在搜索 python3。 I've tried running npm set config /path/to/python , downloading python3, setting the PYTHON path in my.zshrc profile, but nothing seems to be working.我试过运行npm set config /path/to/python ,下载 python3,在 my.zshrc 配置文件中设置 PYTHON 路径,但似乎没有任何效果。 It's entirely possible that my lack of understanding of the issue means that I'm on the right path but didn't quite get something right.完全有可能我对这个问题缺乏了解意味着我走在正确的道路上,但没有完全正确。 Any ideas?有任何想法吗?

This one also plagued me for a week because node-gyp could actually find my Python instance just fine, but then a later build step was not properly configured and all of the popular answers weren't able to pick up Python.这个问题也困扰了我一个星期,因为node-gyp gyp 实际上可以很好地找到我的 Python 实例,但是随后的构建步骤没有正确配置,并且所有流行的答案都无法获取 Python。 My steps to resolve on macOS Monterey ( 12.3.1 )...我在 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

Reading the gyp-node source might helps.阅读gyp-node 源代码可能会有所帮助。 Here are some steps you can try.以下是您可以尝试的一些步骤。

  1. Install python2.安装python2。 You should make sure that in the terminal, which -a python2 only returns one python2 and python2 -V returns the correct 2.x version.您应该确保在终端中, which -a python2只返回一个python2并且python2 -V返回正确的 2.x 版本。

  2. override PYTHON env.覆盖PYTHON export PYTHON=python2 . export PYTHON=python2

  3. Rerun the install.重新运行安装。

If there's still an error, probably the error message is different.如果仍然有错误,则错误消息可能不同。

I believe you can explicitly define env var by prefixing it with npm_config :我相信您可以通过在npm_config前面加上前缀来明确定义 env var:

$ export npm_config_python=/path/to/python

check if that is configured by listing the config:通过列出配置检查是否已配置:

$ npm config list
...

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

this should be picked up by node-gyp .这应该由node-gyp拾取。

Another approach would be to define it in .npmrc另一种方法是在.npmrc中定义它

python = "/path/to/python"

A third approach would be to set it globaly:第三种方法是将其设置为全局:

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

You might be seeing this issue if you upgrade from Node 14 to Node 16, like I did.如果您像我一样从节点 14 升级到节点 16,您可能会看到这个问题。 In that case, a simple workaround for it might be making sure yarn resolves node-sass to version 6.在这种情况下,一个简单的解决方法可能是确保 yarn 将node-sass解析为版本 6。

Set this in your package.json :在你的package.json中设置这个:

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

From the terminal messages, you are installing an old version of node-gyp (node-gyp@3.8.0).从终端消息中,您正在安装旧版本的 node-gyp (node-gyp@3.8.0)。 From a quick search, it seams that this version requires python 2. Python 2 should be present in Big Sur .通过快速搜索,可以看出此版本需要 python 2。 Python 2 应该存在于Big Sur中。 Properly setting the path, should work:正确设置路径,应该可以工作:

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

Also, try:另外,尝试:

export PYTHON=/usr/bin/python

MacOS Monterrey has removed python2 support. MacOS Monterrey 已移除对 python2 的支持。 I have pointed it to python 3 but there's a print statement that doesn't have parentheses so code will always fail !我已经将它指向 python 3 但是有一个没有括号的打印语句,所以代码总是会失败! Any ideas how to get this working I can't do an npm install to get the dependencies任何如何让这个工作的想法我都无法安装 npm 来获取依赖项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM