The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping

--

Chapters
00:00 How To Use A Different Version Of Python During Npm Install?
00:56 Answer 1 Score 62
01:11 Answer 2 Score 8
01:43 Accepted Answer Score 606
02:08 Answer 4 Score 50
02:16 Thank you

--

Full question
https://stackoverflow.com/questions/2045...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #nodejs #centos #npm

#avk47



ACCEPTED ANSWER

Score 606


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




ANSWER 2

Score 62


set python to python2.7 before running npm install

Linux:

export PYTHON=python2.7

Windows:

set PYTHON=python2.7



ANSWER 3

Score 50


For Windows users something like this should work:

PS C:\angular> npm install --python=C:\Python27\python.exe



ANSWER 4

Score 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.