How to use a different version of python during NPM install?

Question:

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”?

Asked By: Nasser Torabzade

||

Answers:

set python to python2.7 before running npm install

Linux:

export PYTHON=python2.7

Windows:

set PYTHON=python2.7
Answered By: Sandtears Kirisame

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.

Answered By: vmx

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 😉 )

Answered By: ack

For Windows users something like this should work:

PS C:angular> npm install --python=C:Python27python.exe
Answered By: Dave L.

for quick one time use this works,
npm install –python=”c:python27″

Answered By: MPV

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:Python27python.exe

//for Linux
npm config set python /usr/bin/python27
Answered By: olu mide
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.