【问题标题】:Running Python on Windows for Node.js dependencies在 Windows 上运行 Python 以获取 Node.js 依赖项
【发布时间】:2013-02-14 02:01:03
【问题描述】:

我正在进入一个 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,设置了变量,然后重新启动,仍然是错误。

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

【问题讨论】:

  • 您可以将文本粘贴为文本而不是图像吗?除了难以阅读(特别是因为它显然缩小了已经低分辨率的位图字体),它不可复制。
  • 更重要的是:重启前你是如何设置环境变量的?如果您刚刚在 cmd 窗口中执行 PYTHON=C:\Python27\Python.exe 并重新启动,则设置丢失。
  • 另外,您安装了哪个 Python 版本?至少旧版本的 waf 和 gyp 需要 2.x 但没有说明任何内容,如果您安装 3.x 会出现各种神秘错误。
  • 感谢您的 cmets。我以文本格式发布了错误。我正在使用 v2.7
  • 好的,但是请回答“更重要”的问题,因为正如正文所暗示的那样,这很重要。您可能还想自己测试一下:在cmd.exe 窗口中,在尝试npm 命令之前,python%PYTHON% 是否启动 Python 解释器?如果没有,则说明您没有正确设置。

标签: python windows node.js npm


【解决方案1】:

什么对我有用:

  • 转到here 并安装所需的python 版本
  • 使用 cmd npm config set python C:\Users\jorge\AppData\Local\Programs\Python\Python310\python.exe 运行以下命令

更改目录的路径

【讨论】:

    【解决方案2】:

    我的问题的答案很简单。

    我在 Python 安装期间打开了一个 Windows 命令窗口。

    安装 Python 后,我尝试从该窗口启动 Python。

    对我来说解决方案很简单。

    当我关闭命令窗口并打开一个新的命令窗口时,问题就消失了。

    我必须打开一个新的命令窗口,以便路径环境变量包含 Python 的路径。

    【讨论】:

      【解决方案3】:

      我尝试了上述所有步骤,但没有一个对我有用。我试图在 windows 10 系统上通过 yarn 安装 argon2 npm 包。

      所以这就是最终的工作

      1. 我运行 npm --add-python-to-path='true' --debug install --global windows-build-tools 在命令终端中以管理员身份安装 python 上面的命令进入了一个无限循环,你可以使用 Ctrl + C 来终止它。
      2. 我通过运行这个设置环境变量 setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"
      3. 我通过运行此命令全局安装了 node-gyp
        npm install --global node-gyp
      4. 我下载了 VS 2019 社区版安装程序,在安装时我添加了使用 C++ 的桌面开发,如图所示
      5. 我再次运行了 yarn 命令。例如yarn add argon2

      希望这些步骤对您有所帮助。我花了几个小时才找到这个解决方案

      【讨论】:

        【解决方案4】:

        我在下载 Akveo ngx-admin 模板后尝试执行“npm install”时遇到了同样的错误。

        这是一种奇怪的情况,因为我只是在另一台笔记本电脑上启动了相同的模板。我意识到我安装了另一个版本的 npm。

        如您所见,我的节点版本是“14.17.0”,第一步我的 npm 版本是“7.13.0”。然后我发现我另一台笔记本电脑的npm版本是“6.14.11”,所以我决定通过执行“npm install -g npm@6.14.11”来降级npm版本。

        然后“npm install”运行良好。

        注意: 无需安装 windows-build-tools 或任何 python 版本。

        【讨论】:

          【解决方案5】:

          如果您没有安装 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 
          

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

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

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

          重新启动 cmd,并通过 set PYTHON 验证变量是否存在,这应该返回变量($env:PYTHON 如果使用 Powershell)

          最后重新申请npm install &lt;module&gt;

          【讨论】:

          • 我有一些权限错误,但设法从以下位置手动安装 python: C:\Users[me]\.windows-build-tools\python.msi 。在安装中可以选择将其添加到路径中。 (重新启动 cmd/PS )它工作了
          • 我得到错误 dh key too small : openssk\ssl\s3_clnt,c:3641
          • 安装 windows-build-tools 需要 PowerShell 在 Windows 10 上以管理员身份运行。这意味着 setx 命令变为 setx PYTHON $env:USERPROFILE\.windows-build-tools\python27\python.exe
          • 我确实必须在提升的命令提示符下运行这些命令,但它似乎奏效了。手动安装 Python 3.x 并将 PYTHON 环境变量设置为指向它不起作用。我假设我的错误与 python 版本控制的差异有关?不管怎样,谢谢!
          • 依赖管理的全部意义在于我们不必必须做这样的事情...... smh
          【解决方案6】:

          如下设置路径即可

          > set PYTHON=D:\\ranjith\\installed\\python-3.6.4\\python.exe
          
          > npm config set python D:\\ranjith\\installed\\python-3.6.4\\python.exe
          

          然后构建你的项目(我喜欢)

          > yarn build
          

          【讨论】:

            【解决方案7】:

            示例:pg_config 不可执行/错误 node-gyp

            解决方案:在 windows 上尝试添加 PATH Env -> C:\Program Files\PostgreSQL\12\bin

            为我工作,现在我可以使用 npm i pg-promise 或其他依赖项。

            【讨论】:

            • pg-promise 你不需要这个。你可能被by this 追上了。只需使用最新版本,你会没事的,不需要任何额外的东西;)
            【解决方案8】:

            这是让 NPM 为您做所有事情的最简单方法

            npm --add-python-to-path='true' --debug install --global windows-build-tools
            

            【讨论】:

              【解决方案9】:

              我在尝试install node-sass@4.9.4 时遇到了同样的挑战。

              在查看了当前的官方文档并阅读了上面的答案后,我注意到您可能不必安装 node-gyp 或安装 windows-build 工具。这就是它所说的,这里是about installing node-gyp on windows。记住 node-gyp 参与了 node-sass 的安装过程。而且您实际上不必重新安装另一个 python 版本。

              这是救星,配置“npm”在安装任何需要构建工具的包时应该查找的 python 路径。

              C:\> npm config set python /Python36/python
              

              我已经在 windows-7 上安装了 python3.6.3。

              【讨论】:

              • 安装node-sass@4.9.4时,我使用node@10.6.3yarn@1.19.1切换到windows-10时,同样的解决方案对我有用
              • 注意Python36/python应该是你的python可执行文件.exe的路径
              • 如果是新的,请转到安装 python 可执行文件的位置。 在 Windows 中,按 hold Shift, and Right-Click 并输入:npm config set python
              【解决方案10】:

              对我来说, 问题是我使用的是 node 的最新版本,而不是 LTS 版本,这是稳定版本,推荐给大多数用户。
              使用LTS 版本解决了这个问题。
              你可以从这里下载:

              LTS Version

              Current Latest Version

              【讨论】:

                【解决方案11】:

                对我来说,这些步骤解决了这个问题:

                1- 以管理员身份运行此 cmd:

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

                2- 第一步完成后运行npm rebuild(尤其是完成python 2.7安装,这是问题的主要原因)

                【讨论】:

                  【解决方案12】:

                  对于我安装 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"
                  

                  已经成功了。

                  【讨论】:

                  • 我还需要上述两个步骤。我还要补充一点,对我来说,(Windows 10 Pro 1803,node v10.15.3,npm v6.9.0)当我运行第一步“npm install windows-build-tools”步骤时,安装似乎从来没有完成后,它似乎在无限循环(将相同的输出行反馈到控制台)。在看了这个看似无休止的循环几分钟后,我选择使用 Ctrl+C 来“结束批处理作业”。一切都按预期进行,中断循环似乎不是问题。
                  【解决方案13】:

                  这有帮助:https://www.npmjs.com/package/node-gyp

                  我按照以下步骤操作:

                  npm install -g node-gyp
                  

                  然后:

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

                  【讨论】:

                  • npm install -g node-gyp 就够了
                  【解决方案14】:

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

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

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

                  【讨论】:

                  • 链接已损坏。
                  • @JuanguiJordán 同时需要的python版本是python3
                  【解决方案15】:

                  正确的方法是 1) 从here 下载并安装python 2.7.14。 2) 从here为python设置环境变量。

                  完成!

                  注意:请相应地设置环境变量。我在这里回答了 windows。

                  【讨论】:

                    【解决方案16】:

                    我不得不提到这一点。 如果您使用的是 Python3 并且使用 node-gyp 失败,那么我很遗憾地告诉您 node-gyp 目前不支持 python3。

                    这里有一个链接给你: https://github.com/nodejs/node-gyp/issues/1268 https://github.com/nodejs/node-gyp/issues/193

                    【讨论】:

                    • 幸运的是,现在没有了。
                    【解决方案17】:

                    有一些解决方案可以解决这个问题: 1 ) 以“管理员”身份运行命令提示符。

                    如果第一个解决方案不能解决您的问题,试试这个:

                    2) 以管理员身份打开命令提示符,粘贴以下代码行并回车:

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

                    【讨论】:

                      【解决方案18】:

                      以下内容以管理员身份从命令行为我工作:

                      安装 windows-build-tools(这可能需要 15-20 分钟):

                       npm --add-python-to-path='true' --debug install --global windows-build-tools
                      

                      添加/更新环境变量:

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

                      安装node-gyp:

                      npm install --global node-gyp
                      

                      将exe文件的名称从Python更改为Python2.7。

                      C:\Users\username\.windows-build-tools\python27\Python2.7
                      

                      npm install module_name --save

                      【讨论】:

                        【解决方案19】:

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

                        【讨论】:

                          【解决方案20】:

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

                          1. C:\Python27\ 添加到您的 PATH 变量中(考虑到您在此目录中安装了 Python)
                            如何设置PATH 环境变量: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

                          请注意,您应该拥有 Python 2.x,而不是 3.x,才能运行基于 node-gyp 的安装!

                          下面的文字是关于 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
                          

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

                          【讨论】:

                            【解决方案21】:

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

                            【讨论】:

                              【解决方案22】:

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

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

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

                              【讨论】:

                                【解决方案23】:

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

                                我遇到了同样的问题,我就这样解决了。

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

                                我希望这会有所帮助。

                                【讨论】:

                                  【解决方案24】:

                                  TL;DR 使用名称 python2.7.exe 复制您的 python.exe 或别名

                                  我的 python 2.7 安装为

                                  D:\app\Python27\python.exe

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

                                  吉普错误!堆栈错误:找不到 Python 可执行文件“python2.7”,您可以设置 PYTHON 环境变量。 吉普错误!堆栈在 failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:103:14)

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

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

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

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

                                  D:\app\Python27&gt;mklink python2.7.exe python.exe

                                  您需要管理员权限才能执行此操作。

                                  【讨论】:

                                    【解决方案25】:

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

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

                                    就是这样!

                                    【讨论】:

                                      【解决方案26】:

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

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

                                      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中再次不同,但你可以google一下.

                                      或者,只需在 npm 命令之前执行 set,中间不要重新启动。


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


                                      这样做有两个问题:

                                      set PYTHON=%PYTHON%;D:\Python
                                      

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

                                      其次,D:\Python 不是您的 Python 解释器的路径。类似于D:\Python\Python.exeD:\Python\bin\Python.exe。找到正确的路径,确保它可以独立运行(例如,输入 D:\Python\bin\Python.exe 并确保您获得 Python 解释器),然后设置变量并使用它。


                                      所以:

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

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

                                      【讨论】:

                                      • 好的,我明白了:Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation。版权所有。 C:\Users\Matt Cashatt>set PYTHON=%PYTHON%;D:\Python C:\Users\Matt Cashatt>%PYTHON% '%PYTHON%' 不是内部或外部命令、可运行程序或批处理文件. C:\Users\Matt Cashatt>
                                      • 另外,D 驱动器是我有 Python 的地方,以防你想知道。再次感谢您的帮助。
                                      • 工作就像一个魅力,谢谢!对于其他人:出于某种原因,我不得不在 NPM 命令窗口中运行set 命令,然后立即运行mpm install jquery 命令。如果我分别做它们,由于某种原因它不起作用。
                                      • @MatthewPatrickCashatt:正如我所解释的,set 命令只会影响当前的cmd 窗口,因此您必须在同一窗口中运行npm 之前执行此操作。如果您想更永久地设置环境变量,可以在系统控制面板中设置,或者您的 Windows 版本的等效设置。
                                      • @abarnert 是这个问题的实际答案,然后设置 PYTHON=D:\Python\bin\Python.exe - 如果您给出了正确的方法并解释了错误的方法,将会很有帮助跨度>
                                      猜你喜欢
                                      • 2016-04-08
                                      • 2013-12-05
                                      • 1970-01-01
                                      • 2015-05-07
                                      • 2016-02-21
                                      • 1970-01-01
                                      • 2020-08-31
                                      • 1970-01-01
                                      • 2016-08-15
                                      相关资源
                                      最近更新 更多