128

I'm trying to run the following command: npm install -g bower gulp cordova ionic tsd@next karma-cli protractor node-gyp coffee-script js-beautify typescript npm-check

I have installed Python, Visual Studio Express and node-gyp so thought I'd be good to go, however I get the following errors:

enter image description here

Regarding the "Can't find Python executable "python", you can set the PYTHON env variable." error, I'm a little confused because I have set the PYTHON environmental variable like so:

enter image description here

Any ideas please?

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
Nick
  • 3,745
  • 20
  • 56
  • 75

18 Answers18

124

Try:

Install all the required tools and configurations using Microsoft's windows-build-tools by running npm install -g windows-build-tools from an elevated PowerShell (run as Administrator).

https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#environment-setup-and-configuration

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
kuan tein
  • 1,345
  • 2
  • 7
  • 7
47

https://github.com/nodejs/node-gyp#on-windows

https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#environment-setup-and-configuration

try

npm config set python D:\Library\Python\Python27\python.exe
Tom Gao
  • 479
  • 4
  • 2
39

I installed python2.7 to solve this issue.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
gus
  • 532
  • 4
  • 6
  • 2
    This fixed my issue...sadly. – Carson May 21 '18 at 22:20
  • 4
    @ZhangBuzz It's because the gyp tool that node-gyp depends on was written for Python 2 and is not compatible with Python 3. – Shweta Gupta Jul 29 '19 at 10:27
  • 9
    2021 here, they still want it aaand the 2.7 cannot be installed due to bug (endless cycle). – tequilacat Feb 19 '21 at 18:18
  • 24
    and now 2022 here! – faza Jan 30 '22 at 14:16
  • A note to all these complainers: that's not how software development works at all. Just because a new year started, it doesn't mean that developers go back and rewrite all their software to the latest language versions. – LachoTomov Mar 08 '22 at 09:25
  • 2
    @LachoTomov get out of the waterfall cycle its agile world, patches and version upgrades are required for fixing bugs and exploits or loose your bitcoin wallets to hippies – MDT Jul 06 '22 at 10:06
  • @MDT I'm well aware of what purpose patches and bug fixes serve, but there's so much more to that than most complaining junior devs here realize. For ex. there's this thing called "minor" and "major" releases - bug fixes usually go into "minor" and that's what you need to upgrade. Python 2.7 to 3 is a major release - and that's a completely different thing. – LachoTomov Jul 07 '22 at 11:07
33

You got to add python to your PATH variable. One thing you can do is Edit your Path variable now and add

;%PYTHON%;

Your variable PYTHON should point to the root directory of your python installation.

jvecsei
  • 1,936
  • 2
  • 17
  • 24
  • 2
    Thank you, I've set the PYTHON path to `C:\Program Files\Python_2.7.10;%PYTHON%;` but I'm still getting the same error unfortunately – Nick Dec 19 '15 at 16:24
  • 2
    Actually you want to set your python path to `C:\Program Files\Python_2.7.10` and then edit your `PATH` Variable and add `;%PYTHON;` . I've looked it up locally and i added `C:\Python34;C:\Python34\Scripts;` to get things working. Although as you see im not using Python 2. – jvecsei Dec 19 '15 at 16:27
  • 1
    Actually this post: http://stackoverflow.com/a/21433154/3378288 describes exactly what i was trying to explain but in a more detailed way ;). For you to note: it's not important if your path variable is `PY_HOME`, or `PYTHON`. It's just important that it matches with what you insert in your path variable. – jvecsei Dec 19 '15 at 16:29
  • Thank you very much, I've set the PYTHON variable properly now and added it into PATH, but still getting the same error... will take a look at the link you posted now :) Cheers – Nick Dec 19 '15 at 16:47
  • 4
    omg... I didn't realise I had to close the command window for the new variables to be in play. Thank you all so much for the replies and advice! – Nick Dec 19 '15 at 17:08
  • 1
    Installing Python2.7.10 and setting the PATH and PYTHON environment variables successfully resolved the issue. Thanks you @jvecsei – Jnana Aug 22 '18 at 09:02
25

Just run below command with admin access

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

12

The easiest way is to let NPM do everything for you,

npm --add-python-to-path='true' --debug install --global windows-build-tools
Shamseer Ahammed
  • 1,787
  • 2
  • 20
  • 25
9

MAC: In my case, I just run the below command and the error resolved:

npm config set python python3
Raja Parivesh
  • 588
  • 6
  • 12
8

I have recently encountered this issue in a dockerfile using node:16-alpine as base image. I have added following run command to fix the issue:

FROM node:alpine as build-stage
RUN apk add --no-cache python3 py3-pip make g++

Here, both tags node:alpine & node:16-alpine are same.

Transhap Higsn
  • 165
  • 4
  • 12
7

My Problem was the usage of Node v16.

I went back to Node v12 (v14 is probably fine as well) and it worked.

Pascal
  • 87
  • 1
  • 1
5

One of the following solutions will work for you:

  1. npm config set python c:\Python\27\python.exe or set PYTHON=D:\Python\bin\Python.exe
  2. npm config set python D:\Library\Python\Python27\python.exe
  3. Let npm configure everything for you (takes forever to complete) npm --add-python-to-path='true' --debug install --global windows-build-tools (Must be executed via "Run As Administrator" PowerShell)

If not... Try to install the required package on your own (I did so, and it was node-sass, after installing it manually, the whole npm install was successfully completed

Elharony
  • 886
  • 8
  • 18
4

You are running the Command Prompt as an admin. You have only defined PYTHON for your user. You need to define it in the bottom "System variables" section.

Also, you should only point the variable to the folder, not directly to the executable.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thank you, I've added `C:\Program Files\Python_2.7.10;%PYTHON%;` in as the PYTHON variable under System variables and unfortunately am still getting the same error :( – Nick Dec 19 '15 at 16:29
  • 1
    As mentioned in the other comments, `%PYTHON%` should go in the PATH variable, not PYTHON. By adding it in PYTHON, you are making a circular reference, which is not good for your computer. – OneCricketeer Dec 19 '15 at 16:32
  • Thank you, have just changed PYTHON and included it in the PATH variable and *still* getting the same error. Argh :( – Nick Dec 19 '15 at 16:39
3

Had this issue on node:18 alpine docker image on Apple Silicon where only python3 is available but at least some versions of node-gyp don't support pyhton3 yet. So I had to use an alpine version that still had python2:

FROM node:18.8.0-alpine3.15

WORKDIR /app

RUN apk --no-cache add python2 make g++

COPY package.json .
COPY yarn.lock .
RUN yarn

COPY . .
thisismydesign
  • 21,553
  • 9
  • 123
  • 126
2
delete node_modules 
delete packagelock.json and yarn.lock(if have)
npm cache clean --force
npm install
Majid joghataey
  • 1,488
  • 2
  • 17
  • 28
1

Run : npm --vs2015 install --global windows-build-tools

itas97
  • 68
  • 9
1
npm config set python D:\Library\Python\Python27\python.exe

This kind off worked for me from Tom Gao's answer

Make sure to change in npmrc file as it doesnt automatically takes \ for the path and do add Python in env var also as mentioned in the answers.

yoyo44
  • 57
  • 5
0

use node version which mentioned in package.json.

using nvm (node version manager) you can switch between respective node version mentions in package.json

Arunkumar
  • 29
  • 2
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32076783) – Caspar V. Jun 26 '22 at 23:17
0

as mentionned by jvecsei

For me adding D:\Softwares\Python\2.7\Scripts instead of D:\Softwares\Python\2.7 to PATH worked.

lMagickl
  • 1
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33091252) – hide1nbush Nov 07 '22 at 08:00
0

npm config set python $(which python)

Tanjin Alam
  • 1,728
  • 13
  • 15