Running Python on Windows for Node.js dependencies

436,766

Solution 1

Your problem is that you didn't set 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 PYTHON variable, it sets the PYTHONPATH variable.


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

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.

Alternatively, just do the set right before the npm command, without rebooting in between.


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.


There are two problems with this:

set PYTHON=%PYTHON%;D:\Python

First, you're setting PYTHON to ;D:\Python. That extra semicolon is fine for a semicolon-separated list of paths, like PATH or 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.

Second, D:\Python is 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.exe and make sure you get a Python interpreter), then set the variable and use it.


So:

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

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

Solution 2

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:

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:

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

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

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 PYTHON which should return the variable ($env:PYTHON if using Powershell)

Lastly re-apply npm install <module>

Solution 3

For me after installing windows-build-tools with the below comment

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

running the code below

npm config set python "%USERPROFILE%\.windows-build-tools\python27\python.exe"

has worked.

Solution 4

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

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.

Solution 5

One and/or multiple of those should help:

  1. Add C:\Python27\ to your PATH variable (considering you have Python installed in this directory)
    How to set PATH env 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 PYTHON and 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

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

The text below says about Unix, but Windows version also requires 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

Share:
436,766

Related videos on Youtube

Matt Cashatt
Author by

Matt Cashatt

I love to code and really enjoy being on Stackoverflow. It has taught me a ton.

Updated on July 29, 2022

Comments

  • Matt Cashatt
    Matt Cashatt over 1 year

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

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

    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.

    Any clue as to what I am missing?

    • abarnert
      abarnert about 11 years
      Can you paste text as text, instead of an image? Besides being hard to read (especially since it's apparently scaled down the already low-res bitmap fonts), it's not copyable.
    • abarnert
      abarnert about 11 years
      More importantly: How did you set the environment variable before rebooting? If you just did PYTHON=C:\Python27\Python.exe in your cmd window and rebooted, the setting was lost.
    • abarnert
      abarnert about 11 years
      Also, which Python version did you install? At least older versions of waf and gyp required 2.x but didn't say anything about it, and would give all kinds of mysterious errors if you installed 3.x instead.
    • Matt Cashatt
      Matt Cashatt about 11 years
      Thanks for your comments. I have posted the error in text format. I am using v2.7
    • abarnert
      abarnert about 11 years
      OK, but please answer the "more importantly" question, because that's, as the text implies, important. You may also want to test this yourself: In the cmd.exe window, before trying the npm command, does either python or %PYTHON% start a Python interpreter? If not, you haven't set it up right.
    • Matt Cashatt
      Matt Cashatt about 11 years
      set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
    • Matt Cashatt
      Matt Cashatt about 11 years
      Of course I used my actual file path ( not My_python_lib).
    • abarnert
      abarnert about 11 years
      You still only answered half of the question, but… that's more than enough to give you the answer.
    • demoncodemonkey
      demoncodemonkey about 8 years
      And if it can't find cl.exe - like it needs for contextify - you'll need to install Visual Studio with C++ too as mentioned here stackoverflow.com/questions/31953769/…
  • Matt Cashatt
    Matt Cashatt about 11 years
    OK, so I get this:Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Matt Cashatt>set PYTHON=%PYTHON%;D:\Python C:\Users\Matt Cashatt>%PYTHON% '%PYTHON%' is not recognized as an internal or external command, operable program or batch file. C:\Users\Matt Cashatt>
  • Matt Cashatt
    Matt Cashatt about 11 years
    Also, the D drive is where I have Python in case you are wondering. Thanks again for your help.
  • Matt Cashatt
    Matt Cashatt about 11 years
    Worked like a charm, thanks! For others: I had to run the set command in the NPM command window for some reason and then run the mpm install jquery command right after that. If I did them separately it didn't work for some reason.
  • abarnert
    abarnert about 11 years
    @MatthewPatrickCashatt: As I explained, the set command only affects the current cmd window, so you have to do it before running npm in the same window. If you want to set environment variables more permanently, that's in the System Control Panel, or equivalent for your version of Windows.
  • reach4thelasers
    reach4thelasers about 10 years
    @abarnert is the actual answer to this question then set PYTHON=D:\Python\bin\Python.exe - would be helpful if you gave the correct way as well as explaining the wrong way
  • d.raev
    d.raev over 6 years
    I had some permissions error, but managed to manually install python from: C:\Users[me]\.windows-build-tools\python.msi . In the installation is an option to add it to the path. (Restarting the cmd/PS ) and it worked
  • user1428716
    user1428716 over 6 years
    I get the error dh key too small : openssk\ssl\s3_clnt,c:3641
  • Bae
    Bae over 6 years
    Installing windows-build-tools required PowerShell run as Admin on Windows 10. This means the setx command becomes setx PYTHON $env:USERPROFILE\.windows-build-tools\python27\python.exe
  • FrozenKiwi
    FrozenKiwi about 6 years
    keep in mind: requires Python2.7, 3.X is not supported.
  • JeffryHouser
    JeffryHouser almost 6 years
    I did have to run these commands in an elevated command prompt, but it seems to have worked. Manually installing Python 3.x and setting the PYTHON environment variable to point to that did not work. I assume my error related to the difference in python versioning? Thanks regardless!
  • StackOverflowUser
    StackOverflowUser about 5 years
    I also needed both of the above steps. I'll also add that, for me, (Windows 10 Pro 1803, node v10.15.3, npm v6.9.0) when I ran the first step, the 'npm install windows-build-tools' step, that install never seemed to complete, it seemed to be looping endlessly (feeding back the same output lines to the console). After watching this seeming endless loop for a number of minutes, I elected to use Ctrl+C to "end the batch job". Everything worked as intended, interrupting the loop didn't appear to be a problem.
  • Chamin  Thilakarathne
    Chamin Thilakarathne over 4 years
    npm install -g node-gyp was enough
  • vitaly-t
    vitaly-t over 4 years
    For pg-promise you do not need this. You probably were caught up by this. Just use the latest version, and you will be fine, no need for any of that extra stuff ;)
  • MwamiTovi
    MwamiTovi over 4 years
    Same solution worked for me while installing [email protected], when i switched to windows-10, using [email protected] and [email protected]
  • MwamiTovi
    MwamiTovi over 4 years
    Note that Python36/python should be the path to your python executable file .exe
  • MwamiTovi
    MwamiTovi about 4 years
    If new, goto where your python executable was installed. In windows, Press hold Shift, and Right-Click and enter this: npm config set python
  • Juangui Jordán
    Juangui Jordán almost 4 years
    The link is unfortunately broken.
  • lcnittl
    lcnittl over 3 years
    @JuanguiJordán In the meantime, the required python version is python3
  • lcnittl
    lcnittl over 3 years
    Not anymore, luckily.
  • Tom Pietrosanti
    Tom Pietrosanti almost 3 years
    The whole point of dependency management is so we don't have to do stuff like this... smh
  • KLMN
    KLMN over 2 years
    npm --vs2015 install --global windows-build-tools resolve my problem. (github.com/felixrieseberg/windows-build-tools/issues/208)
  • Brandon Hollenbeck
    Brandon Hollenbeck over 2 years
    I was getting a similar error to OP and I had to set the Python path as well. I set it like this " npm config set python C:\Users\ben\.windows-build-tools\python27\python.exe "
  • kaushalpranav
    kaushalpranav over 2 years
    windows-build-tools is corrupting my anaconda installation by installing a separate python 2.7. For my problem, updating npm version from 6 -> 8, resolved the issue without asking for any python at all. So, I would suggest updating npm before installing windows-build-tools
  • sylargaf
    sylargaf almost 2 years
    2022, Windows 10, this worked! Thanks