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

Question:

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:UsersJakub>set PYTHON=C:MYSELFProgramsPython2.7python.exe

C:UsersJakub>echo %PYTHON%
C:MYSELFProgramsPython2.7python.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:UsersJakub>npm install minecraft-protocol



> [email protected] install C:UsersJakubnode_modulesminecraft-protocolnode_modulesursa
> node-gyp rebuild

|
C:UsersJakubnode_modulesminecraft-protocolnode_modulesursa>if not defined npm_config_node_gyp (node "C:Program Files (x86)nodejsnode_modulesnpmbinnode-gyp-bin\....no
de_modulesnode-gypbinnode-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)nodejsnode_modulesnpmnode_modulesnode-gyplibconfigure.js:103:14)
gyp ERR! stack     at C:Program Files (x86)nodejsnode_modulesnpmnode_modulesnode-gyplibconfigure.js:64:11
gyp ERR! stack     at FSReqWrap.oncomplete (evalmachine.<anonymous>:95:15)
Asked By: Tomáลก Zato

||

Answers:

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

Answered By: KlwntSingh

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

My python 2.7 was installed as

D:appPython27python.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 Filesnodejsnode_modulesnpmnode_modulesnode-gyplibconfigure.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:appPython27>mklink python2.7.exe python.exe

You need admin rights for this operation.

Answered By: iaarnio

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

npm config set python C:ProgramsPython2.7python2.7.exe

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

Answered By: Tomáลก Zato

This worked for me:

npm config set python C:Users<username>.windows-build-toolspython27python.exe

Set the path to python executable accordingly.
Hope this help ๐Ÿ™‚

Answered By: LAXIT KUMAR

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

Answered By: Anubhav Gupta

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

Answered By: Pankaj Toshniwal
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.