即使设置了 %PYTHON%,Node.js (npm) 也拒绝找到 python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31251367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:41:29  来源:igfitidea点击:

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

javascriptpythonnode.jsnpm

提问by Tomá? Zato - Reinstate Monica

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

所以我试图让 Node.js 工作。当然,这并不像宣传的那么容易:)

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 PYTHONenvironment variable with this error:

我的电脑上碰巧有两个 python 版本,但 Node.js 似乎只适用于旧版本 2.7。出现错误时,它还鼓励我将路径设置为PYTHON带有此错误的环境变量:

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 echoto check whether the variable was really set. Unfortunatelly, that npmthing can't read it and the error appears again. Here's the full log right after I set the %PYTHON%variable:

可以看到我之前echo是检查变量是否真的设置了。不幸的是,那个npm东西无法读取,错误再次出现。这是我设置%PYTHON%变量后的完整日志:

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)

采纳答案by Tomá? Zato - Reinstate Monica

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

我发现最稳定的解决方案是将pythonnpm 内部值设置为实际路径:

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.

这将跳过所有环境变量和%PATH%废话,并在安装的任何地方启动 python。

回答by KlwntSingh

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

在 Windows 的情况下设置环境变量后重新打开终端,但在 linux 的情况下无需重新启动终端。

回答by iaarnio

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

TL;DR 使用名称 python2.7.exe 制作 python.exe 的副本或别名

My python 2.7 was installed as

我的 python 2.7 安装为

D:\app\Python27\python.exe

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

无论我如何设置(和验证)PYTHON 环境变量,我总是遇到此错误:

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:

这样做的原因是在 node-gyp 的 configure.js 中,python 可执行文件被解析为:

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

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.

结果证明 gyp.opts.python 的值为 'python2.7',从而覆盖了 process.env.PYTHON。

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

我通过为 python.exe 可执行文件创建一个别名来解决这个问题,名称为 node-gyp 正在寻找:

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

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

You need admin rights for this operation.

您需要此操作的管理员权限。

回答by LAXIT KUMAR

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 :-)

相应地设置 python 可执行文件的路径。希望这有帮助:-)

回答by Anubhav Gupta

On Windows goto directory C:\Users\<<your username>>\.windows-build-tools\python27and copy and paste the python.exeas python.2.7.exein the same directory.

在 Windows 上转到目录C:\Users\<<your username>>\.windows-build-tools\python27并将python.exeas复制并粘贴python.2.7.exe到同一目录中。