在 Windows 上为 Node.js 依赖项运行 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15126050/
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-18 13:25:10  来源:igfitidea点击:

Running Python on Windows for Node.js dependencies

pythonwindowsnode.jsnpm

提问by Matt Cashatt

I am getting into a Node.js codebase which requires that I download a few dependencies via NPM, namely jQuery.

我正在进入一个 Node.js 代码库,它要求我通过 NPM(即 jQuery)下载一些依赖项。

In attempting to run npm install jquery, I keep getting this error:

在尝试运行时npm install jquery,我不断收到此错误:

Your environment has been set up for using Node.js 0.8.21 (x64) and NPM

C:\Users\Matt Cashatt>npm install jquery
npm http GET https://registry.npmjs.org/jquery
npm http 304 https://registry.npmjs.org/jquery
npm http GET https://registry.npmjs.org/jsdom
npm http GET https://registry.npmjs.org/xmlhttprequest
npm http GET https://registry.npmjs.org/htmlparser/1.7.6
npm http GET https://registry.npmjs.org/location/0.0.1
npm http GET https://registry.npmjs.org/navigator
npm http GET https://registry.npmjs.org/contextify
npm http 304 https://registry.npmjs.org/htmlparser/1.7.6
npm http 304 https://registry.npmjs.org/xmlhttprequest
npm http 304 https://registry.npmjs.org/location/0.0.1
npm http 304 https://registry.npmjs.org/navigator
npm http 304 https://registry.npmjs.org/jsdom
npm http 304 https://registry.npmjs.org/contextify
npm http GET https://registry.npmjs.org/bindings
npm http GET https://registry.npmjs.org/cssom
npm http GET https://registry.npmjs.org/cssstyle
npm http GET https://registry.npmjs.org/request
npm http 304 https://registry.npmjs.org/bindings

> [email protected] install C:\Users\Matt Cashatt\node_modules\jquery\node_module
s\contextify
> node-gyp rebuild


C:\Users\Matt Cashatt\node_modules\jquery\node_modules\contextify>node "C:\Progr
am Files\nodejs\node_modules\npm\bin\node-gyp-bin\..\..\node_modules\node-gyp\b
in\node-gyp.js" rebuild
npm http 304 https://registry.npmjs.org/cssstyle
npm http 304 https://registry.npmjs.org/cssom
npm http 304 https://registry.npmjs.org/request
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYT
HON env variable.
gyp ERR! stack     at failNoPython (C:\Program Files\nodejs\node_modules\npm\nod
e_modules\node-gyp\lib\configure.js:113:14)
gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node
-gyp\lib\configure.js:82:11
gyp ERR! stack     at Object.oncomplete (fs.js:297:15)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\Program Files\nodejs\node_modules\npm\node_modu
les\node-gyp\bin\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\Matt Cashatt\node_modules\jquery\node_modules\contextify
gyp ERR! node -v v0.8.21
gyp ERR! node-gyp -v v0.8.4
gyp ERR! not ok
npm ERR! error rolling back Error: ENOTEMPTY, rmdir 'C:\Users\Matt Cashatt\node_
modules\jquery\node_modules\jsdom\node_modules\request\tests'
npm ERR! error rolling back  [email protected] { [Error: ENOTEMPTY, rmdir 'C:\Users\M
att Cashatt\node_modules\jquery\node_modules\jsdom\node_modules\request\tests']
npm ERR! error rolling back   errno: 53,
npm ERR! error rolling back   code: 'ENOTEMPTY',
npm ERR! error rolling back   path: 'C:\Users\Matt Cashatt\node_modules\jque
ry\node_modules\jsdom\node_modules\request\tests' }
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! `cmd "/c" "node-gyp rebuild"` failed with 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is most likely a problem with the contextify 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 their info via:
npm ERR!     npm owner ls contextify
npm ERR! There is likely additional logging output above.

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\Program Files\nodejs\\node.exe" "C:\Program Files\nod
ejs\node_modules\npm\bin\npm-cli.js" "install" "jquery"
npm ERR! cwd C:\Users\Matt Cashatt
npm ERR! node -v v0.8.21
npm ERR! npm -v 1.2.11
npm ERR! code ELIFECYCLE
npm ERR! Error: ENOENT, lstat 'C:\Users\Matt Cashatt\node_modules\jquery\node_mo
dules\jsdom\node_modules\request\tests\test-pipes.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\Program Files\nodejs\\node.exe" "C:\Program Files\nod
ejs\node_modules\npm\bin\npm-cli.js" "install" "jquery"
npm ERR! cwd C:\Users\Matt Cashatt
npm ERR! node -v v0.8.21
npm ERR! npm -v 1.2.11
npm ERR! path C:\Users\Matt Cashatt\node_modules\jquery\node_modules\jsdom\node_
modules\request\tests\test-pipes.js
npm ERR! fstream_path C:\Users\Matt Cashatt\node_modules\jquery\node_modules\jsd
om\node_modules\request\tests\test-pipes.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack C:\Program Files\nodejs\node_modules\npm\node_modules\fst
ream\lib\writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:297:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Users\Matt Cashatt\npm-debug.log
npm ERR! not ok code 0

C:\Users\Matt Cashatt>

It looks like the failure is due to a missing Python installation. Well, I have installed Python, set the variable, and rebooted and still the error.

看起来失败是由于缺少 Python 安装。好吧,我已经安装了 Python,设置了变量,然后重新启动,但仍然是错误。

Any clue as to what I am missing?

关于我缺少什么的任何线索?

采纳答案by abarnert

Your problem is that you didn'tset the environment variable.

你的问题是你没有设置环境变量。

The error clearly says this:

错误清楚地说明了这一点:

gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.

And in your comment, you say you did this:

在您的评论中,您说您这样做了:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

That's nice, but that doesn't set the PYTHONvariable, it sets the PYTHONPATHvariable.

这很好,但这并没有设置PYTHON变量,而是设置了PYTHONPATH变量。



Meanwhile, just using the setcommand only affects the current cmdsession. If you reboot after that, as you say you did, you end up with a whole new cmdsession that doesn't have that variable set in it.

同时,仅使用该set命令只会影响当前cmd会话。如果你在那之后重新启动,就像你说的那样,你最终会得到一个cmd没有设置该变量的全新会话。

There are a few ways to set environment variables permanently—the easiest is in the System Control Panel in XP, which is of course different in Vista, different again in 7, and different again in 8, but you can google for it.

有几种方法可以永久设置环境变量——最简单的方法是在 XP 中的系统控制面板中,这当然在 Vista 中不同,在 7 中再次不同,在 8 中再次不同,但您可以谷歌搜索。

Alternatively, just do the setright before the npmcommand, without rebooting in between.

或者,只需setnpm命令之前执行正确的操作,而无需在两者之间重新启动。



You can test whether you've done things right by doing the exact same thing the config script is trying to do: Before running npm, try running %PYTHON%. If you've done it right, you'll get a Python interpreter (which you can immediately quit). If you get an error, you haven't done it right.

您可以通过执行配置脚本尝试执行的完全相同的操作来测试您是否做对了:在运行之前npm,尝试运行%PYTHON%. 如果你做对了,你会得到一个 Python 解释器(你可以立即退出)。如果你得到一个错误,你没有做对。



There are two problems with this:

这有两个问题:

set PYTHON=%PYTHON%;D:\Python

First, you're setting PYTHONto ;D:\Python. That extra semicolon is fine for a semicolon-separated list of paths, like PATHor PYTHONPATH, but not for a single value like PYTHON. And likewise, adding a new value to the existing value is what you want when you want to add another path to a list of paths, but not for a single value. So, you just want set PYTHON=D:\Python.

首先,您要设置PYTHON;D:\Python. 额外的分号适用于以分号分隔的路径列表,例如PATHPYTHONPATH,但不适用于像PYTHON. 同样,当您想要将另一个路径添加到路径列表而不是单个值时,向现有值添加新值是您想要的。所以,你只想要set PYTHON=D:\Python.

Second, D:\Pythonis not the path to your Python interpreter. It's something like D:\Python\Python.exe, or D:\Python\bin\Python.exe. Find the right path, make sure it works on its own (e.g., type D:\Python\bin\Python.exeand make sure you get a Python interpreter), then set the variable and use it.

其次,D:\Python不是你的 Python 解释器的路径。它类似于D:\Python\Python.exe, 或D:\Python\bin\Python.exe。找到正确的路径,确保它自己工作(例如,输入D:\Python\bin\Python.exe并确保你得到一个 Python 解释器),然后设置变量并使用它。



So:

所以:

set PYTHON=D:\Python\bin\Python.exe

Or, if you want to make it permanent, do the equivalent in the Control Panel.

或者,如果您想使其永久化,请在控制面板中执行等效操作。

回答by Jon Leavitt

Here is a guide that resolved a lot of these issues for me.

这是一个为我解决了很多这些问题的指南。

http://www.steveworkman.com/node-js/2012/installing-jsdom-on-windows/

http://www.steveworkman.com/node-js/2012/installing-jsdom-on-windows/

I remember in particular the python version as important. Make sure you install 2.7.3 instead of 3's.

我特别记得 python 版本很重要。确保安装 2.7.3 而不是 3。

回答by zhekaus

I had the same issue and none of these answers did help. In my case PYTHON variable was set correctly. However python was installed too deep, i.e. has too long path. So, I did the following:

我遇到了同样的问题,这些答案都没有帮助。在我的情况下,PYTHON 变量设置正确。但是python安装的太深,即路径太长。所以,我做了以下事情:

  1. reinstalled python to c:\python
  2. set environmental variable PYTHON to C:\python\python.exe
  1. 重新安装 python 到 c:\python
  2. 将环境变量 PYTHON 设置为 C:\python\python.exe

And that's it!

就是这样!

回答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 Projenix

Here is the correct command: set path=%path%;C:\Python34 [Replace with the correct path of your python installation]

这是正确的命令:set path=%path%;C:\Python34 [替换为你的python安装的正确路径]

I had the same problem and I just solved this like that.

我有同样的问题,我就是这样解决的。

As some other people pointed out, this is volatile configuration, it only works for the current cmd session, and (obviously) you have to set your path before you run npm install.

正如其他一些人指出的那样,这是易变的配置,它仅适用于当前的 cmd 会话,并且(显然)您必须在运行 npm install 之前设置路径。

I hope this helps.

我希望这有帮助。

回答by Y. Aliaksei

gyp ERR! configure error gyp ERR! stack Error: Can't find Python executable "python", you can set the PYT HON env variable.

吉普错误!配置错误gyp ERR!堆栈错误:找不到 Python 可执行文件“python”,您可以设置 PYT HON 环境变量。

Not necessary to reinstall, this exception throw by node-gyp script, then try to rebuild. It's enough setup environment variable like in my case I did:

不需要重新安装,这个异常由 node-gyp 脚本抛出,然后尝试重建。像我这样设置环境变量就足够了:

SET PYTHON=C:\work\_env\Python27\python.exe

回答by emigenix

If you're trying to use this on Cygwin, then you need to follow the instructions in thisanswer. (It's a problem how Cygwin treats Windows symlinks.)

如果您尝试在 Cygwin 上使用它,则需要按照答案中的说明进行操作。(这是 Cygwin 如何处理 Windows 符号链接的问题。)

回答by thybzi

One and/or multiple of those should help:

其中一个和/或多个应该有助于:

  1. Add C:\Python27\to your PATHvariable (considering you have Python installed in this directory)
    How to set PATHenv variable: http://www.computerhope.com/issues/ch000549.htm
    Restart your console and/or Windows after setting variable.

  2. In the same section as above ("Environment Variables"), add new variable with name PYTHONand value C:\Python27\python.exe
    Restart your console and/or Windows after setting variable.

  3. Open Windows command line (cmd) in Admin mode.
    Change directory to your Python installation path: cd C:\Python27
    Make symlink needed for some installations: mklink python2.7.exe python.exe

  1. 添加C:\Python27\到您的PATH变量(考虑到您在此目录中安装了 Python)
    如何设置PATHenv 变量:http: //www.computerhope.com/issues/ch000549.htm
    设置变量后重新启动您的控制台和/或 Windows。

  2. 在与上述相同的部分(“环境变量”)中,添加具有名称PYTHON和值的新变量C:\Python27\python.exe
    在设置变量后重新启动控制台和/或 Windows。

  3. 在管理员模式下打开 Windows 命令行 ( cmd) 。 将目录更改为您的 Python 安装路径: 制作某些安装所需的符号链接:
    cd C:\Python27
    mklink python2.7.exe python.exe

Please note that you should have Python 2.x, NOT 3.x, to run node-gypbased installations!

请注意,您应该使用 Python 2.x 而不是 3.x来运行node-gyp基于安装的安装!

The text below says about Unix, but Windows version also requires Python 2.x:

下面的文字是关于 Unix 的,但 Windows 版本也需要 Python 2.x:

You can install with npm:

$ npm install -g node-gyp
You will also need to install:

On Unix:
python (v2.7 recommended, v3.x.x is not supported)
make
A proper C/C++ compiler toolchain, like GCC

This article may also help: https://github.com/nodejs/node-gyp#installation

这篇文章也可能有帮助:https: //github.com/nodejs/node-gyp#installation

回答by benscabbia

If you haven't got python installed along with all the node-gyp dependencies, simply open Powershell or Git Bash with administrator privileges and execute:

如果您还没有安装 python 以及所有 node-gyp 依赖项,只需使用管理员权限打开 Powershell 或 Git Bash 并执行:

npm install --global --production windows-build-tools

and then to install the package:

然后安装软件包:

npm install --global node-gyp

once installed, you will have all the node-gyp dependencies downloaded, but you still need the environment variable. Validate Python is indeed found in the correct folder:

安装后,您将下载所有 node-gyp 依赖项,但您仍然需要环境变量。验证 Python 确实在正确的文件夹中找到:

C:\Users\ben\.windows-build-tools\python27\python.exe 

Note - it uses python 2.7 not 3.x as it is not supported

注意 - 它使用 python 2.7 而不是 3.x,因为它不受支持

If it doesn't moan, go ahead and create your (user) environment variable:

如果它没有呻吟,请继续创建您的(用户)环境变量:

setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"

restart cmd, and verify the variable exists via set PYTHONwhich should return the variable

重新启动cmd,并验证变量是否存在,通过set PYTHON它应该返回变量

Lastly re-apply npm install <module>

最后重新申请 npm install <module>

回答by T-prod

Why not downloading the python installer here? It make the work for you when you check the path installation

为什么不在这里下载 python 安装程序?当您检查路径安装时,它会为您工作