簡體   English   中英

在 Windows 上運行 Python 以獲取 Node.js 依賴項

[英]Running Python on Windows for Node.js dependencies

我正在進入一個 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,設置了變量,然后重新啟動,仍然是錯誤。

關於我缺少什么的任何線索?

如果您沒有安裝 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,因為它不受支持*

如果它沒有抱怨,請繼續創建您的(用戶)環境變量:

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

重新啟動 cmd,並通過set PYTHON驗證變量是否存在,它應該返回變量( $env:PYTHON如果使用 Powershell)

最后重新應用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 中再次不同,但你可以 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

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

對我來說,在安裝 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"

已經奏效了。

這是一個為我解決了很多這些問題的指南。

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

我記得特別重要的是 python 版本。 確保安裝 2.7.3 而不是 3。

其中一個和/或多個應該有助於:

  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

我有同樣的問題,這些答案都沒有幫助。 在我的情況下,PYTHON 變量設置正確。 然而python安裝得太深,即路徑太長。 所以,我做了以下事情:

  1. 將 python 重新安裝到 c:\python
  2. 將環境變量 PYTHON 設置為 C:\python\python.exe

就是這樣!

這有幫助: https ://www.npmjs.com/package/node-gyp

我按照以下步驟操作:

npm install -g node-gyp

然后:

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

有一些解決方案可以解決這個問題:1)以“管理員”身份運行命令提示符。

如果第一個解決方案不能解決您的問題,請嘗試以下解決方案:

2)以管理員身份打開命令提示符,粘貼以下代碼行並回車:

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

我在嘗試安裝 node-sass@4.9.4時遇到了同樣的挑戰。

在查看了當前的官方文檔並閱讀了上面的答案之后,我注意到您可能不必安裝 node-gyp 或安裝 windows-build 工具。 這就是它所說的,這里是關於在 windows 上安裝 node-gyp 記住 node-gyp 參與了 node-sass 的安裝過程。 而且您實際上不必重新安裝另一個 python 版本。

這是救星,配置“npm”在安裝任何需要構建工具的包時應該查找的 python 路徑。

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

我已經在 windows-7 上安裝了 python3.6.3。

以下內容以管理員身份從命令行為我工作:

安裝 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"

安裝節點gyp:

npm install --global node-gyp

將 exe 文件的名稱從 Python 更改為 Python2.7。

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

npm install module_name --save

這是讓 NPM 為您做所有事情的最簡單方法

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

TL;DR 使用名稱 python2.7.exe 復制您的 python.exe 或別名

我的 python 2.7 安裝為

D:\app\Python27\python.exe

無論我如何設置(和驗證)PYTHON env 變量,我總是得到這個錯誤:

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)

原因是在 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>mklink python2.7.exe python.exe

您需要管理員權限才能執行此操作。

我不禁要提到這一點。 如果您使用的是 Python3 並且使用 node-gyp 失敗,那么我很遺憾地告訴您 node-gyp 目前不支持 python3。

這是一個鏈接: https ://github.com/nodejs/node-gyp/issues/1268 https://github.com/nodejs/node-gyp/issues/193

正確的方法是 1) 從這里下載並安裝 python 2.7.14。 2)從這里為python設置環境變量。

完畢!

注意:請相應地設置環境變量。 我在這里回答了windows。

對我來說,這些步驟解決了這個問題:

1-以管理員身份運行此 cmd:

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

2-然后在第一步完成后運行npm rebuild (尤其是完成 python 2.7 安裝,這是問題的主要原因)

為什么不在這里下載 python 安裝程序? 當您檢查路徑安裝時,它會為您工作

我嘗試了上述所有步驟,沒有一個對我有用。 我試圖在 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

我希望這些步驟對您有所幫助。 我花了幾個小時才找到這個解決方案

這是正確的命令: set path=%path%;C:\Python34 [替換為您的 python 安裝的正確路徑]

我有同樣的問題,我只是這樣解決了這個問題。

正如其他一些人指出的那樣,這是可變配置,它僅適用於當前的 cmd 會話,並且(顯然)您必須在運行 npm install 之前設置路徑。

我希望這有幫助。

吉普錯誤! 配置錯誤gyp ERR! 堆棧錯誤:找不到 Python 可執行文件“python”,您可以設置 PYT HON 環境變量。

不需要重新安裝,這個異常由 node-gyp 腳本拋出,然后嘗試重建。 這是足夠的設置環境變量,就像我所做的那樣:

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

如果您嘗試在 Cygwin 上使用它,那么您需要按照答案中的說明進行操作。 (Cygwin 如何處理 Windows 符號鏈接是一個問題。)

示例:pg_config 不可執行 / 錯誤 node-gyp

解決方案:在 Windows 上嘗試添加 PATH Env -> C:\Program Files\PostgreSQL\12\bin

為我工作,現在我可以使用 npm i pg-promise 或其他依賴項。

什么對我有用:

  • 這里安裝需要的python版本
  • 使用 cmd npm config set python C:\Users\jorge\AppData\Local\Programs\Python\Python310\python.exe運行以下命令

更改目錄的路徑

對我來說,問題是我使用的是 node 的最新版本,而不是LTS版本,它是穩定版本,推薦給大多數用戶。
使用LTS版本解決了這個問題。
您可以從這里下載:

LTS 版本

當前最新版本

設置如下路徑,它將起作用

> 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

在下載 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 版本。

https://www.python.org/downloads/下載最新版本安裝時選中了Add Python 3.10 to PATH選項。 重新啟動后,一切正常。

我的問題的答案很簡單。

在 Python 安裝期間,我打開了一個 Windows 命令窗口。

安裝 Python 后,我嘗試從該窗口啟動 Python。

對我來說解決方案很簡單。

當我關閉命令窗口並打開一個新的命令窗口時,問題就消失了。

我必須打開一個新的命令窗口,以便路徑環境變量包含 Python 的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM