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

7

73

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?

Burnoose answered 24/11, 2021 at 14:45 Comment(7)
You're saying that export PYTHON=/usr/bin/python3 did not fix this? – Restriction
@TimRoberts I'm assuming you mean by adding that to my .zshrc? I do have that in my zsh and it's still not working. – Burnoose
You should be able to type that at a command line before you run the yarn command. If that flies, then that gives you a clue. – Restriction
@TimRoberts I see. I ran that in my terminal and yarn install produces the same error unfortunately. – Burnoose
The python in /usr/local/opt is managed by homebrew. Can you try removing /usr/local/opt from your PATH, at least temporarily? Alternatively, you can prepend /usr/bin to your path like this: PATH=/usr/bin:$PATH yarn install ... – Parachronism
Hi @jakub, that appears to be giving the same error still. – Burnoose
Does /usr/local/opt/[email protected]/bin/python3 exist ? – Belabor
L
33

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.

Larynx answered 7/12, 2021 at 6:2 Comment(2)
This appears to have fixed it! I knew I'd been close this whole time, but your step 2 seems to have been the one that I haven't tried. Thanks! – Burnoose
Yeah, I was kinda bit surprised when reading the code, the PYTHON env will get appended with PATH no matter what, so putting the full path of python there is just wrong. That's a bit weird and easily explains your problem, but sure it may be just because one of the dependencies in your project uses old node-gyp. – Larynx
T
148

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
$ source ~/.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
Therapeutic answered 21/4, 2022 at 16:28 Comment(8)
thank you for the response. I am also using macOS Monterey (12.3.1) and one of the packages I am installing is looking exclusively for python2. Can you help me understand, how I can address this? node --version: v16.14.2 and npm --version: 8.5.0 – Biblioclast
@DeepakThota try using the the answer above, but replace 3.10.3 with 2.7.18. If python2 is needed... try to also point python2 to the newly installed version of python. export python2=python – Therapeutic
Yes using pyenv install 2.7.18 and exporting it worked. Thank you! – Biblioclast
If you want you can add python2 to be set localy for specific folder. Go inside that folder and run pyenv local 2.7.18 – Knout
open a new terminal window/tab so you see which python command is working – Intolerant
exec source ~/. zshrc after editing it – Milagro
Thanks a lot. Sometimes we should have both versions of Python: 2.x & 3.x. – Friedrick
this export -> export PATH=\"\${HOME}/.pyenv/shims:\${PATH}\" in my zshrc is broking my terminal. Seems like I have a lot of export PATH already there. – Wendling
L
33

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.

Larynx answered 7/12, 2021 at 6:2 Comment(2)
This appears to have fixed it! I knew I'd been close this whole time, but your step 2 seems to have been the one that I haven't tried. Thanks! – Burnoose
Yeah, I was kinda bit surprised when reading the code, the PYTHON env will get appended with PATH no matter what, so putting the full path of python there is just wrong. That's a bit weird and easily explains your problem, but sure it may be just because one of the dependencies in your project uses old node-gyp. – Larynx
M
32

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"
  }
Mycosis answered 18/6, 2022 at 7:27 Comment(3)
This is the only thing that worked for me. – Deste
Increasing the node-sass version in my package.json made it work. Theres a compatibility table on the repos readme (github.com/sass/node-sass). I guess overwriting the resolutions is only necessary if node-sass is a transient dependency? – Guarneri
It worked!!! thanks. for me in node 18 it was "resolutions": { "node-sass": "^8.0.0" } – Thwack
B
8

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
Broder answered 6/12, 2021 at 12:22 Comment(0)
D
0

From the terminal messages, you are installing an old version of node-gyp ([email protected]). From a quick search, it seams that this version requires python 2. Python 2 should be present in Big Sur. Properly setting the path, should work:

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

Also, try:

export PYTHON=/usr/bin/python
Disseminule answered 4/12, 2021 at 15:54 Comment(0)
T
0

I was also having the same issue. Tried almost everything. Switching the node version using nvm. Restarting. Trying to install a different node-sass version. Nothing worked for me.

Instead of trying to get node-sass work. You can just migrate to dart-sass(sass). There is practically no code change except for the import lines.

In my file. The changes were.

sass.compiler = require('node-sass');

to

sass.compiler = require('sass');

and

const sass = require('gulp-sass')

to

const sass = require('gulp-sass')(require('sass'))

Check the following article as well.

https://sass-lang.com/blog/libsass-is-deprecated#how-do-i-migrate

Hope this helps someone.

Timberwork answered 6/12, 2022 at 1:57 Comment(0)
S
0

This how I fixed the issue

$ open ~/.zshrc

Add python from brew

alias python3="/opt/homebrew/bin/python3"

to fix NPM

export PYTHON="/opt/homebrew/bin/python3"
Strident answered 6/12, 2023 at 18:29 Comment(0)

© 2022 - 2025 β€” McMap. All rights reserved.