Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js (npm) refuses to find python even after %PYTHON% has been set

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
\


> [email protected] 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)
like image 581
Tomáš Zato - Reinstate Monica Avatar asked Jul 06 '15 16:07

Tomáš Zato - Reinstate Monica


People also ask

Can't find Python executable Python You can set the Python?

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.

Does node-gyp require Python 2?

node-gyp requires that you have installed a compatible version of Python, one of: v3. 7, v3. 8, v3. 9, or v3.


2 Answers

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.

like image 163
Tomáš Zato - Reinstate Monica Avatar answered Oct 09 '22 22:10

Tomáš Zato - Reinstate Monica


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.

like image 40
iaarnio Avatar answered Oct 09 '22 22:10

iaarnio