Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

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. 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 [email protected]
gyp info using [email protected] | darwin | x64
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "/usr/local/opt/[email protected]/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. 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. 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?

like image 943
J. Jackson Avatar asked Nov 24 '21 14:11

J. Jackson


People also ask

Where is Python executable macOS?

The Apple-provided build of Python is installed in /System/Library/Frameworks/Python. framework and /usr/bin/python , respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software.

Can't find Python executable Python You can set the Python Env Variable NPM install?

To Solve Can't find Python executable “python”, you can set the PYTHON env variable Error You just need to Install Python In Your Windows ( If Not Installed ) and then add python to your PATH variable. Using environment variable. OR You can set npm config set python path and it will also Solve your error.

How do I install Python on npm?

One of the following solutions will work for you: 1 npm config set python c:Python27python.exe or set PYTHON=D:PythonbinPython.exe 2 npm config set python D:LibraryPythonPython27python.exe 3 Let npm configure everything for you (takes forever to complete) npm --add-python-to-path='true' --debug install... More ...

How do I install node in Ubuntu Big Sur?

Install Node manually, by Homebrew, by nvm and also downgrade npm to various lower versions. Reinstall Homebrew to get fresh background as it was used before upgrade to Big Sur. Reinstall xcode command-line tools. Run the node and map the project in the docker container ubuntu:focal.

How do I fix Python2 installation error 2?

Install 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. override PYTHON env. export PYTHON=python2. Rerun the install. If there's still an error, probably the error message is different. This appears to have fixed it!


4 Answers

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. My steps to resolve on 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
like image 69
PaulMest Avatar answered Oct 28 '22 03:10

PaulMest


Reading the gyp-node source might helps. Here are some steps you can try.

  1. Install 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.

  2. override PYTHON env. export PYTHON=python2.

  3. Rerun the install.

If there's still an error, probably the error message is different.

like image 18
willnode Avatar answered Oct 28 '22 01:10

willnode


You might be seeing this issue if you upgrade from Node 14 to Node 16, like I did. In that case, a simple workaround for it might be making sure yarn resolves node-sass to version 6.

Set this in your package.json:

 "resolutions": {
    "node-sass": "^6.0.1"
  }
like image 10
Jan Klimo Avatar answered Oct 28 '22 01:10

Jan Klimo


I believe you can explicitly define env var by prefixing it with npm_config:

$ 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.

Another approach would be to define it in .npmrc

python = "/path/to/python"

A third approach would be to set it globaly:

npm config --global set python /path/to/python
like image 5
Pedreiro Avatar answered Oct 28 '22 02:10

Pedreiro