在Windows上运行Python以满足Node.js依赖
我正在学习一个Node.js的代码项目,这个项目需要我通过NPM下载一些依赖包,特别是jQuery。
我尝试运行 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
> contextify@0.1.4 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 jquery@1.8.3 { [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! contextify@0.1.4 install: `node-gyp rebuild`
npm ERR! `cmd "/c" "node-gyp rebuild"` failed with 1
npm ERR!
npm ERR! Failed at the contextify@0.1.4 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! <npm-@googlegroups.com>
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>
看起来这个错误是因为缺少Python的安装。其实我已经安装了Python,设置了相关的环境变量,并且重启了电脑,但问题还是没有解决。
有没有人知道我还缺少什么呢?
26 个回答
对我来说,在用下面的命令安装了windows-build-tools之后,
npm --add-python-to-path='true' --debug install --global windows-build-tools
运行下面的代码
npm config set python "%USERPROFILE%\.windows-build-tools\python27\python.exe"
就成功了。
如果你还没有安装Python以及所有node-gyp需要的依赖,首先打开管理员权限的Powershell或Git Bash,然后执行:
npm install --global --production windows-build-tools
接着安装这个包:
npm install --global node-gyp
安装完成后,你就会下载到所有node-gyp需要的依赖,但你还需要设置环境变量。确认一下Python确实在正确的文件夹里:
C:\Users\ben\.windows-build-tools\python27\python.exe
*注意 - 它使用的是Python 2.7,而不是3.x,因为3.x不被支持*
如果没有报错,那就继续创建你的(用户)环境变量:
setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"
重启命令行窗口,然后通过 set PYTHON
来验证这个变量是否存在,如果你在使用Powershell,可以用 $env:PYTHON
来查看
最后重新运行 npm install <module>
来安装模块
你的问题是你没有设置环境变量。
错误信息很清楚地说明了这一点:
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
在你的评论中,你说你做了这个:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
这很好,但这并没有设置PYTHON
变量,而是设置了PYTHONPATH
变量。
另外,使用set
命令只会影响当前的cmd
会话。如果你在这之后重启了电脑,正如你所说的那样,你会得到一个全新的cmd
会话,而这个会话里并没有设置那个变量。
有几种方法可以永久设置环境变量——最简单的是在XP的系统控制面板里,这在Vista、7和8中都不一样,但你可以在网上查一下。
另外,你可以在运行npm
命令之前,直接使用set
命令,而不需要重启。
你可以通过做配置脚本试图做的事情来测试你是否做对了:在运行npm
之前,试着运行%PYTHON%
。如果你做对了,你会看到一个Python解释器(你可以立刻退出)。如果出现错误,那说明你没有做对。
set PYTHON=%PYTHON%;D:\Python
首先,你把PYTHON
设置成了;D:\Python
。那个多余的分号在像PATH
或PYTHONPATH
这样的路径列表中是可以的,但对于像PYTHON
这样的单一值就不行了。而且,想要在路径列表中添加新值时,才需要在已有值后面加上新值,但对于单一值就不需要。所以,你只需要set PYTHON=D:\Python
。
第二,D:\Python
并不是你的Python解释器的路径。它应该是类似D:\Python\Python.exe
或者D:\Python\bin\Python.exe
的路径。找到正确的路径,确保它可以单独工作(例如,输入D:\Python\bin\Python.exe
,确保你能看到Python解释器),然后再设置变量并使用它。
所以:
set PYTHON=D:\Python\bin\Python.exe
或者,如果你想让它永久生效,可以在控制面板中做相应的设置。