274

I have terminal access to a VPS running centos 5.9 and default python 2.4.3 installed. I also installed python 2.7.3 via these commands: (I used make altinstall instead of make install)

wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar -xf Python-2.7.3.tgz
cd Python-2.7.3
./configure
make
make altinstall

then I installed node.js from source via these commands:

python2.7 ./configure
make
make install

The problem is, when I use npm install and try to install a node.js package which requires python > 2.4.3 I get this error:

gyp ERR! configure error
gyp ERR! stack Error: Python executable "python" is v2.4.3, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
gyp ERR! stack     at failPythonVersion (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:125:14)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:114:9

how should I "pass the --python switch to point to Python >= v2.5.0"?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nasser Torabzade
  • 6,490
  • 8
  • 27
  • 36
  • I would propose a hack. How about aliasing this python version inside your shell & then after you close the shell, the system should return to previous state – rafee Dec 08 '13 at 13:58
  • @rafee it's great, can you explain how can I do that? I'm a bit new to centos! – Nasser Torabzade Dec 08 '13 at 14:02
  • I almost never used CentOS. It should be just like any other linux distribution. I am telling about temporary procedure, because making alias permanent may damage your system. So that shouldn't be done. – rafee Dec 08 '13 at 14:04
  • @rafee thanks, I'll check your solution. – Nasser Torabzade Dec 08 '13 at 14:09
  • 2
    alias python=/path/to/python should be good enough. But be careful about making the change temporary, permanent change may harm your system – rafee Dec 08 '13 at 14:09

6 Answers6

600

You can use --python option to npm like so:

npm install --python=python2.7

or set it to be used always:

npm config set python python2.7

Npm will in turn pass this option to node-gyp when needed.

(note: I'm the one who opened an issue on Github to have this included in the docs, as there were so many questions about it ;-) )

ack
  • 7,356
  • 2
  • 25
  • 20
  • If any Python 2 version is acceptable, can one use `npm install --python=python2`? – Freedom_Ben Sep 22 '14 at 18:57
  • 2
    Sure, if `python2` is on your $PATH, use that, by all means :-) – ack Oct 04 '14 at 23:54
  • 6
    For `pyenv` with Python 3, I've used `npm install --python=/usr/bin/python` – Andrei Jun 24 '15 at 11:08
  • 12
    as i have python3 as global, i need to setup also python27 using: > this npm config set python C:\Python27\python.exe – francois Jan 23 '16 at 23:22
  • @francois this is exactly what I did and it worked wonderfully. – Josh McKearin Jan 24 '16 at 17:16
  • 2
    Note that you can also specify the path of the version of Python that you want to use. This is useful if you have more than one version of the same Python version installed: `npm config set python /path/to/executable/python2.7` – Moot Sep 13 '17 at 17:16
  • this doesn't work for me with nvm on Mac, I had to use the environement variable – maxymoo Dec 09 '18 at 22:58
  • Another solution without the CLI is to edit directly the .npmrc file and add the line: `python=path/to/executable/python.exe` – 3pwd Sep 30 '19 at 11:43
  • i used "npm install react-redux --python=python2.7" – Andrii Jan 11 '20 at 12:45
  • 4
    Neither the `--python=python3` flag nor `npm config set python python3` worked for me. My NPM insists on using Python 2 for installation. Maybe it's different for global NPM packages? Has anyone had luck with another approach? – mercer721 Oct 12 '20 at 19:35
  • @mercer721 the same happened to me although I specified the exact python version like so npm i --python=python3.9. The reason it was failing for me was that my current node version (switched to it using nvm) is 10 and one package needs to be compiled with node-gyp. From what I understand my current version of node (node10) works with python2 only. I hope this helps – bboydflo Apr 14 '21 at 08:21
  • I just installed `node` and only use `python3`, otherwise, I will not work with node in production. Node devs are to me `high potentials`, but not being able without python2 is `lowpotential`, correct me if I am wrong. – Timo May 14 '21 at 18:14
  • @AlessandroPezzato What is the final solution by now for `python3`? – Timo May 14 '21 at 18:15
  • works even with yarn. I have been able to fix the issue with `yarn config set python python2.7` thank you for the answer – Artem Nov 08 '21 at 18:39
  • If it still doesn't work, you can check .npmrc file (located in home dir or in AppData/Roaming/npm/node_modules/npm if you are using global config). Mine has this line: python=C:\Python27\python.exe – milos Mar 21 '23 at 11:14
62

set python to python2.7 before running npm install

Linux:

export PYTHON=python2.7

Windows:

set PYTHON=python2.7
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • 4
    `export` is used to set a environment variable, and the variable works on your shell and its child process.. – Sandtears Kirisame Dec 08 '13 at 14:19
  • my one or this one? because if my one worked, then I would post that as answer & that should be marked as answer – rafee Dec 08 '13 at 16:40
  • @rafee this one worked, I saved your suggestion for future needs, thanks. – Nasser Torabzade Dec 09 '13 at 13:48
  • 7
    use `set PYTHON=python2.7` on Windows – zacharyliu Feb 28 '14 at 11:10
  • 3
    This is a bad idea when its necessary that there is a certain python3 environment loaded. python2.7 is only used for npm in this case, so only set it for npm and not for your whole shell environment without knowing what you are doing. – ohcibi Aug 03 '15 at 09:09
  • This is a hackish solution. In a professional setting where you work on many projects these types of fixes would quickly become counter productive. – Timothy Gonzalez Jan 24 '23 at 18:37
49

For Windows users something like this should work:

PS C:\angular> npm install --python=C:\Python27\python.exe
Dave L.
  • 9,595
  • 7
  • 43
  • 69
  • 12
    This can be combined with the accepted answer using: `npm config set python C:\Python27\python.exe` - I just did this and it worked fine, which means not having to do it every time you want to install something. – Nathan Ridley Nov 30 '15 at 23:58
  • 1
    If you're using Git Bash, you need to escape the backslashes: `npm install --python=C:\\Python27\\python.exe`. – Leponzo Jul 29 '21 at 13:32
  • I had to wrap the path in double quotes, npm install --python="C:\Python27\python.exe" for git bash – Busman Nov 26 '21 at 15:43
18

This one works better if you don't have the python on path or want to specify the directory :

//for Windows
npm config set python C:\Python27\python.exe

//for Linux
npm config set python /usr/bin/python27
olu mide
  • 307
  • 3
  • 11
8

Ok, so you've found a solution already. Just wanted to share what has been useful to me so many times;

I have created setpy2 alias which helps me switch python.

alias setpy2="mkdir -p /tmp/bin; ln -s `which python2.7` /tmp/bin/python; export PATH=/tmp/bin:$PATH"

Execute setpy2 before you run npm install. The switch stays in effect until you quit the terminal, afterwards python is set back to system default.

You can make use of this technique for any other command/tool as well.

vmx
  • 7,989
  • 4
  • 22
  • 20
3

for quick one time use this works, npm install --python="c:\python27"

MPV
  • 337
  • 3
  • 10