18

So I am trying to get the Node.js to work. Of course, it's not as easy as advertised :)

I happen to have two python versions on my computer, but Node.js seems to only work with the older one, 2.7. Upon error, it also encourages me to set the path into PYTHON environment variable with this error:

Error: Can't find Python executable "python2.7", you can set the PYTHON env variable.

Ok then, I configured the variable as desired:

C:\Users\Jakub>set PYTHON=C:\MYSELF\Programs\Python2.7\python.exe

C:\Users\Jakub>echo %PYTHON%
C:\MYSELF\Programs\Python2.7\python.exe

You can see that I used echo to check whether the variable was really set. Unfortunatelly, that npm thing can't read it and the error appears again. Here's the full log right after I set the %PYTHON% variable:

C:\Users\Jakub>npm install minecraft-protocol
\


> ursa@0.8.5 install C:\Users\Jakub\node_modules\minecraft-protocol\node_modules\ursa
> node-gyp rebuild

|
C:\Users\Jakub\node_modules\minecraft-protocol\node_modules\ursa>if not defined npm_config_node_gyp (node "C:\Program Files (x86)\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\no
de_modules\node-gyp\bin\node-gyp.js" rebuild )  else (rebuild)
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python2.7", you can set the PYTHON env variable.
gyp ERR! stack     at failNoPython (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:103:14)
gyp ERR! stack     at C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:64:11
gyp ERR! stack     at FSReqWrap.oncomplete (evalmachine.<anonymous>:95:15)
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Changes to environment variables using "set" only last for the duration of the cmd window. – Rob Foley Jul 06 '15 at 16:53
  • I didn't close the window - the `npm install ...` was issued right after configuring the variable, in the same window. – Tomáš Zato Jul 06 '15 at 16:55
  • I had a similar issue on Windows 10. I ended up uninstalling Node, Python, Electron, and NPM, restarting my computer, reinstalling Node, Python, Electron, and NPM, then restarting my computer again before it finally worked. gyp + windows = joke – Shawn Whinnery Nov 16 '17 at 00:33

7 Answers7

58

I figured out the most stable solution is to set python npm internal value to actual path:

npm config set python C:\Programs\Python2.7\python2.7.exe

This skips all environment variable and %PATH% crap and just starts the python wherever it's installed.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
7

TL;DR Make a copy or alias of your python.exe with name python2.7.exe

My python 2.7 was installed as

D:\app\Python27\python.exe

I always got this error no matter how I set (and verified) PYTHON env variable:

gyp ERR! stack Error: Can't find Python executable "python2.7", you can set the PYTHON env variable.
gyp ERR! stack     at failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:103:14)

The reason for this was that in node-gyp's configure.js the python executable was resolved like:

var python = gyp.opts.python || process.env.PYTHON || 'python'

And it turned out that gyp.opts.python had value 'python2.7' thus overriding process.env.PYTHON.

I resolved this by creating an alias for python.exe executable with name node-gyp was looking for:

D:\app\Python27>mklink python2.7.exe python.exe

You need admin rights for this operation.

iaarnio
  • 399
  • 4
  • 9
  • had the same approach but that didn't solve the problem for me. – DoubleU23 Mar 14 '16 at 20:17
  • 2
    @DoubleU23, I recommend using npm config as [Tomáš Zato](http://stackoverflow.com/users/607407/tom%c3%a1%c5%a1-zato) suggested. That seems like the official way. My suggestion is more a workaround. – iaarnio Mar 15 '16 at 22:08
2

Reopen your terminal after you set your environment variable in case of windows but in case of linux no need to restart terminal.

KlwntSingh
  • 1,084
  • 8
  • 26
1

This worked for me:

npm config set python C:\Users\<username>\.windows-build-tools\python27\python.exe

Set the path to python executable accordingly. Hope this help :-)

LAXIT KUMAR
  • 459
  • 6
  • 7
1

On Windows goto directory C:\Users\<<your username>>\.windows-build-tools\python27 and copy and paste the python.exe as python.2.7.exe in the same directory.

Anubhav Gupta
  • 1,176
  • 11
  • 12
1

I tried everything but none worked for me, then I installed python using this Link at this location C:\Python27\python.exe

0

It was admin vs local user issue for me !

Please see my steps in different thread, here

techloris_109
  • 547
  • 5
  • 13