Electron-builder: node-gyp fails build because it can't find python2

Created on 3 Aug 2016  路  9Comments  路  Source: electron-userland/electron-builder

  • 5.22.1:

  • mac:

I have an electron project which uses a python 3 environment for a segment of the app and thus the entirety of my repo's environment is python 3.

When I run build -m --x64 to build for Mac (or the equivalent for windows) I get an error that node-gyp can't find python2. I know that node-gyp is not compatible with python3.. I normally have npm set to use python 2 for node-gyp with:npm config set python /usr/bin/python2.7

And anytime I use npm within a python 3 environment I don't get an error about node-gpy not finding python2.

Also, within my project which has python 3.4.3 I can run electron-packager and create windows/mac builds without any errors from node-gyp being unable to find python 2.

But when I run electron-builder it seems that my environment has to be set to python 2. Two question I have are,

  1. How can I tell electron-builder the path to python 2 when it invokes node-gyp/electron-packager (if I understand how things work internally and I may not)...
  2. How is that electron-packager can package the same app in a python 3 environment but electron-builder needs the environments python to be strictly python 2 to work...

Right now I build a segment of my application using python 3 then switch to python 2 to build the rest since I've switched from electron-packager to electron-builder..

I can write scripts locally and on our CI (travis, appveyor) that do this python version switching but I'm hoping that won't be unnecessary. For now I'll have to do that to finish my work..

Below is the node-gyp error I get because my environment is in python 3,

$ build -m --x64                            
Rebuilding app dependencies for arch x64 to /Users/moi/Code/foo/otone_frontend/app

gyp ERR! configure error
gyp ERR! stack Error: Command failed: /opt/boxen/pyenv/shims/python2 -c import platform; print(platform.python_version());
gyp ERR! stack pyenv: python2: command not found
gyp ERR! stack
gyp ERR! stack The `python2' command exists in these Python versions:
gyp ERR! stack   2.7.5
gyp ERR! stack   2.7.7
gyp ERR! stack   2.7.9
gyp ERR! stack
gyp ERR! stack
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:202:12)
gyp ERR! stack     at emitTwo (events.js:106:13)
gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
gyp ERR! stack     at maybeClose (internal/child_process.js:850:16)
gyp ERR! stack     at Socket.<anonymous> (internal/child_process.js:323:11)
gyp ERR! stack     at emitOne (events.js:96:13)
gyp ERR! stack     at Socket.emit (events.js:188:7)
gyp ERR! stack     at Pipe._handle.close [as _onclose] (net.js:485:12)
gyp ERR! System Darwin 13.4.0
gyp ERR! command "/opt/boxen/nodenv/versions/v6.0.0/bin/node" "/opt/boxen/nodenv/versions/v6.0.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/moi/Code/foo/otone_frontend/app/node_modules/bufferutil
gyp ERR! node -v v6.0.0
gyp ERR! node-gyp -v v3.3.1
gyp ERR! not ok

npm ERR! Darwin 13.4.0
npm ERR! argv "/opt/boxen/nodenv/versions/v6.0.0/bin/node" "/opt/boxen/nodenv/versions/v6.0.0/bin/npm" "rebuild" "--production"
npm ERR! node v6.0.0
npm ERR! npm  v3.8.6
npm ERR! code ELIFECYCLE
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the bufferutil package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs bufferutil
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls bufferutil
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/moi/Code/foo/otone_frontend/app/npm-debug.log
Error: npm exited with code 1
    at ChildProcess.childProcess.on.code (/Users/moi/Code/foo/otone_frontend/node_modules/electron-builder/src/util/util.ts:132:14)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:850:16)
    at Socket.<anonymous> (internal/child_process.js:323:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:485:12)
From previous event:
    at spawn (/Users/moi/Code/foo/otone_frontend/node_modules/electron-builder/src/util/util.ts:109:9)
    at spawnNpmProduction (/Users/moi/Code/foo/otone_frontend/node_modules/electron-builder/src/util/util.ts:43:9)
    at Object.installDependencies (/Users/moi/Code/foo/otone_frontend/node_modules/electron-builder/src/util/util.ts:21:120)
    at Packager.<anonymous> (/Users/moi/Code/foo/otone_frontend/node_modules/electron-builder/src/packager.ts:214:34)
    at next (native)
    at tryOnImmediate (timers.js:543:15)
    at processImmediate [as _immediateCallback] (timers.js:523:5)
From previous event:
    at tsAwaiter (/Users/moi/Code/foo/otone_frontend/node_modules/electron-builder/src/util/awaiter.ts:10:47)
    at Object.build (/Users/moi/Code/foo/otone_frontend/node_modules/electron-builder/src/builder.ts:195:52)
    at Object.<anonymous> (/Users/moi/Code/foo/otone_frontend/node_modules/electron-builder/src/build-cli.ts:27:28)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:159:18)
    at node.js:444:3
backlog help wanted investigate

Most helpful comment

I could've sworn this didn't work for me the first time, but running this solved the issue:

pyenv shell "2.7.10"

error I was getting:

gyp ERR! configure error 
gyp ERR! stack Error: Command failed: /Users/.../.pyenv/shims/python2 -c import platform; print(platform.python_version());
gyp ERR! stack pyenv: python2: command not found
gyp ERR! stack 
gyp ERR! stack The `python2' command exists in these Python versions:
gyp ERR! stack   2.7.10

All 9 comments

I can run electron-packager and create windows/mac builds without any errors from node-gyp being unable to find python 2.

Because electron-package doesn't rebuild native deps, but electron-builder does it for you.

npm config set python /usr/bin/python2.7

Very strange. Because we do call npm as cli, so, config must be respected. To investigate.

Moved to backlog to keep issue list clear.

I discovered the issue that caused this. I use pyenv (in a project that has python 3 code) to set the python virtualenv environment. Pyenv shims edit the process PATH variable in such a way that no python 2 executable is reachable once a pyenv virtualenv python 3 environment is activated. I don't fully understand the way pyenv shims work at the moment. But I did confirm that this issue is not related to electron-builder but rather the env I'm running electron builder in. After running my build scripts in CI where I'm not using pyenv to manage python environments electron-builder worked correctly because it had access to both the python 2 and 3 without any interference.

@SimplyAhmazing how did you config pyenv CI to both python 2 and 3

I made my build script activate a Python 2 environment when it needed py2.

I discovered this issue today. How is this fixed?

I could've sworn this didn't work for me the first time, but running this solved the issue:

pyenv shell "2.7.10"

error I was getting:

gyp ERR! configure error 
gyp ERR! stack Error: Command failed: /Users/.../.pyenv/shims/python2 -c import platform; print(platform.python_version());
gyp ERR! stack pyenv: python2: command not found
gyp ERR! stack 
gyp ERR! stack The `python2' command exists in these Python versions:
gyp ERR! stack   2.7.10

Recent pyenv versions (unsure about older ones) also allow to essentially activate two python versions simultaneously. We've resolved this on our project with pyenv local 3.7.0 2.7.10.

This error was resolved for my by running $ pyenv shell 3.7.1 2.7.15

I encountered this issue trying to run npx create-react-app my_app --use-npm

For my system with yarn installed, create-react-app assumes yarn is the default package manager and create-react-app succeeds.

But I wanted to force it to use npm with the --use-npm flag, and doing so generates the error documented in this thread.

Running $ pyenv shell 3.7.1 2.7.15 resolved the issue. See below for more insight.

$ pyenv versions
  system
  2.7.15
* 3.7.1 (set by /Users/(me)/.pyenv/version)

$ pyenv shell 3.7.1 2.7.15

$ pyenv versions
  system
* 2.7.15 (set by PYENV_VERSION environment variable)
* 3.7.1 (set by PYENV_VERSION environment variable)

$ pyenv shell
3.7.1:2.7.15

# now create-react-app succeeds!
npx create-react-app my_app --use-npm
Was this page helpful?
0 / 5 - 0 ratings