npm - "Can't find Python executable "python", you can set the PYTHON env variable."
Asked Answered
J

20

135

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?

Jackhammer answered 19/12, 2015 at 16:1 Comment(7)
Is there an underscore in your actual path?Psycholinguistics
Thanks @PadraicCunningham, yes there is i.imgur.com/1kuv8Pw.pngJackhammer
Can you post the full evn exactly as you have it set? You can also add it to your PATHPsycholinguistics
Thanks, I've set it to C:\Program Files\Python_2.7.10\python.exeJackhammer
Set it to your folder with the executable, C:\Program Files\Python_2.7.10 or add to your PATH https://mcmap.net/q/79960/-how-to-add-to-the-pythonpath-in-windows-so-it-finds-my-modules-packages-duplicate under system variablesPsycholinguistics
Ah! That makes sense, thank you very much. Still getting the same error though unfortunately, even with it set to C:\Program Files\Python_2.7.10Jackhammer
no prob, you're welcome. You can add and accept an answer that worked for youPsycholinguistics
A
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.

Alleviator answered 19/12, 2015 at 16:7 Comment(6)
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 unfortunatelyJackhammer
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.Alleviator
Actually this post: https://mcmap.net/q/79960/-how-to-add-to-the-pythonpath-in-windows-so-it-finds-my-modules-packages-duplicate 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.Alleviator
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 :) CheersJackhammer
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!Jackhammer
Installing Python2.7.10 and setting the PATH and PYTHON environment variables successfully resolved the issue. Thanks you @AlleviatorUnwieldy
N
129

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

Nonna answered 30/1, 2018 at 7:34 Comment(7)
This did the trick on my behalf. Pretty annoying though that it will install Python 2.7 as I don't want to soil my environment with a Python version that I'm not going to use.Schober
It should be the perfect answer.Krp
Fixed this error, and probably others I haven't hit yet. Going into the list of dev machine setup steps!Sharpset
is system restart required after that? it appears yes for me to get workEllata
FYI - that command will reboot your machine during installAye
the command currently returns this response "[email protected]: Node.js now includes build tools for Windows. You probably no longer need this tool. See github.com/felixrieseberg/windows-build-tools for details."Disinfect
windows-build-tools is not deprecated.Hueston
M
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
Mannerheim answered 5/9, 2017 at 15:42 Comment(2)
This resulted in npm ERR! python is not a valid npm optionVervet
the 'python' option was removed with npm v9 - see #74523456.Jehu
C
40

I installed python2.7 to solve this issue.

Conative answered 12/12, 2017 at 7:59 Comment(7)
This fixed my issue...sadly.Hittite
@ZhangBuzz It's because the gyp tool that node-gyp depends on was written for Python 2 and is not compatible with Python 3.Conceal
2021 here, they still want it aaand the 2.7 cannot be installed due to bug (endless cycle).Lysine
and now 2022 here!Ellata
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.Catfish
@Catfish 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 hippiesObidiah
@Obidiah 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.Catfish
A
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.

Alleviator answered 19/12, 2015 at 16:7 Comment(6)
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 unfortunatelyJackhammer
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.Alleviator
Actually this post: https://mcmap.net/q/79960/-how-to-add-to-the-pythonpath-in-windows-so-it-finds-my-modules-packages-duplicate 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.Alleviator
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 :) CheersJackhammer
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!Jackhammer
Installing Python2.7.10 and setting the PATH and PYTHON environment variables successfully resolved the issue. Thanks you @AlleviatorUnwieldy
C
26

Just run below command with admin access

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

Childers answered 22/5, 2020 at 19:1 Comment(2)
The package windows-build0tools is deprecated: npmjs.com/package/windows-build-toolsRunlet
It shows me "Downloading installers failed. Error: TypeError: 'process.env' only accepts a configurable, writable, and enumerable data descriptor" using npm v9.6.7 with Node.js v18.17.1Kory
U
12

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

npm --add-python-to-path='true' --debug install --global windows-build-tools
Unmentionable answered 5/4, 2019 at 5:11 Comment(1)
Don't think this is merged in yet, and it didn't work on this end - github.com/nodejs/node-gyp/pull/1109Sublime
E
12

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

npm config set python python3
Either answered 7/1, 2023 at 5:40 Comment(2)
This one introduce more errors like ` Missing parentheses in call to 'print'. Did you mean print`Indecorum
npm ERR! python` is not a valid npm option`Buxton
C
9

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.

Cobelligerent answered 11/5, 2021 at 10:47 Comment(1)
add RUN apk add --no-cache python3 py3-pip make g++ to docker file solved my issue, thnx!Methodology
A
8

My Problem was the usage of Node v16.

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

Alonsoalonzo answered 11/5, 2021 at 16:59 Comment(1)
Any idea why this fixes it? Does it not use python?Zuleika
S
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

Sideband answered 16/3, 2020 at 14:36 Comment(1)
First installing node-sass manually actually worked for me. Had been struggling with python thing.Godolphin
C
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.

Clingstone answered 19/12, 2015 at 16:7 Comment(3)
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 :(Jackhammer
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.Clingstone
Thank you, have just changed PYTHON and included it in the PATH variable and still getting the same error. Argh :(Jackhammer
N
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 . .
Niagara answered 31/8, 2022 at 10:59 Comment(0)
C
2

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

Clariceclarie answered 13/8, 2021 at 7:32 Comment(1)
perfect answer since this fixes the missing python2.7 issue tooSweyn
R
2
delete node_modules 
delete packagelock.json and yarn.lock(if have)
npm cache clean --force
npm install
Role answered 12/9, 2022 at 12:23 Comment(0)
J
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.

Jenine answered 26/1, 2022 at 16:42 Comment(0)
A
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

Acadia answered 22/6, 2022 at 6:27 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewEnenstein
C
0

as mentionned by jvecsei

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

Champaign answered 2/11, 2022 at 19:35 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewBash
S
0

npm config set python $(which python)

Smacker answered 3/5, 2023 at 7:41 Comment(0)
B
0

On macOS installing Python 2 solves this:

With asdf, in a .tool-versions file:

python 2.7.18

Then

asdf plugin add python
asdf install
Buxton answered 29/1 at 8:48 Comment(0)
U
0

For ubuntu:

sudo apt-get update
sudo apt install python2

Check python 2 version:

python2 --version
Unadvised answered 16/4 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.