47

When i install iconv with npm got the following error:

iconv@2.1.0 install /root/Dropbox/nodeApps/nodeApp/node_modules/iconv node-gyp rebuild

gyp ERR! configure error 
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at failNoPython (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:103:14)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:42:11
gyp ERR! stack     at F (/usr/local/lib/node_modules/npm/node_modules/which/which.js:43:25)
gyp ERR! stack     at E (/usr/local/lib/node_modules/npm/node_modules/which/which.js:46:29)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/which/which.js:57:16
gyp ERR! stack     at Object.oncomplete (fs.js:107:15)
gyp ERR! System Linux 3.8.0-19-generic
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /root/Dropbox/nodeApps/nodeApp/node_modules/iconv
gyp ERR! node -v v0.10.28
gyp ERR! node-gyp -v v0.13.0
gyp ERR! not ok 

npm ERR! iconv@2.1.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the iconv@2.1.0 install script.
npm ERR! This is most likely a problem with the iconv package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls iconv
npm ERR! There is likely additional logging output above.
npm ERR! System Linux 3.8.0-19-generic
npm ERR! command "node" "/usr/local/bin/npm" "i"
npm ERR! cwd /root/Dropbox/nodeApps/nodeApp
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.10
npm ERR! code ELIFECYCLE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /root/Dropbox/nodeApps/nodeApp/npm-debug.log
npm ERR! not ok code 0

although I installed python and can run it from console:

# python
Python 2.7.3 (default, May  9 2014, 12:18:32) 
[GCC 4.8.2] on linux2

and set up PATH in ~/.bashrc:

export PYTHONPATH=$PYTHONPATH:/Python-2.7.3
export PATH=$PATH:/Python-2.7.3

and done . ~/.bashrc

Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166
  • the problem was because of `~/.bashrc` was not loaded on ssh login. I put `PATH` vars to `~/.bash_profile` and it's ok – Maxim Yefremov May 09 '14 at 18:50
  • I had the PYTHON var set in ~/.zshrc but kept getting the error after restarting iTerm, so I ended up with this symlink hack: $ which python /usr/bin/python $ sudo ln -s /usr/bin/python /usr/local/bin/python – kwcto Jan 28 '16 at 07:21

7 Answers7

101

For anyone encountering this issue on Ubuntu 16.04...
node-gyp can't use Python 3.5.X which seems to the be the default that ships with 16.04. I read somewhere that 16.04 was supposed to ship with Python2 as well but I can't find it on my install.

I fixed the above issue by:

apt-get update     
apt-get install python2.7    
ln -s /usr/bin/python2.7 /usr/bin/python 

Now when node-gyp goes looking for python it will hit your Python2.7 install and load correctly.

ern
  • 1,522
  • 13
  • 19
Chase
  • 3,009
  • 3
  • 17
  • 23
  • You can't put `ln` in bashrc though. It requires sudo access. How do you do this permanently? – towi_parallelism Jul 03 '19 at 12:58
  • 2
    @towi_parallelism You can set the `PYTHON` environment variable. Do it like this: `export PYTHON="$(which python2.7)"`. It does seem rather silly that node-gyp is still locked to Python v2.7 when it's being retired in just 5 months...... – starbeamrainbowlabs Jul 16 '19 at 15:21
  • 1
    I am giving an up vote to this answer. I am using ubuntu 20.04 in which python 2.7 was not installed. Main python version is 3.8. I am using angular cli 9. I was getting this error after npm install " import sys; print "%s.%s.%s" % sys.version_info[:3];" and found out that there is syntax change in newer version of python. Based on that, I installed pyhon 2.7 and set npm config python path to 2.7. I worked after that – Nirav Shah Sep 27 '21 at 07:47
11

In your bash session where you're able to just type python and get a valid response, type in which python and note the full path location of the python binary. Take that location and put that into your PYTHONPATH and PATH environment variables, except without the python at the end.

For example, which python gives me:

/usr/local/bin/python

so I would write:

export PYTHONPATH=$PYTHONPATH:/usr/local/bin
export PATH=$PATH:/usr/local/bin

in my ~/.bashrc.

huu
  • 7,032
  • 2
  • 34
  • 49
10

There is a simple and safe way Place this into ~/.bashrc or ~/.bash_aliases file:

alias python=python3

After adding the above in the file, run source ~/.bashrc or source ~/.bash_aliases

This solution worked for my Ubuntu see the origin answer here

Kelei Ren
  • 360
  • 4
  • 10
7

Install python2.7.
apt install python2

Mohamed Gnedy
  • 87
  • 1
  • 1
  • 1
    In the question, it is explicitly stated that python2.7 is already installed and can be run from the shell using `python`. – He3lixxx Jun 01 '21 at 12:59
  • I didn't have the problem that OP had, but I was seeing something similar, and this fix did the trick. (In my case, I'm on Ubuntu and didn't have Python2 installed.) – Eric Walker May 04 '22 at 23:01
2

the problem was because of ~/.bashrc was not loaded on ssh login. I put PATH vars to ~/.bash_profile and it's ok

Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166
2

Sometimes it is not only related to NPM. So, a system-wide solution is required. Then, just create a system-wide symlink for 'python' to your current python path.

For example, for my Ubuntu 20 setup (be root):

ln -s /usr/bin/python3 /usr/bin/python

Then verify the symlink:

ls -l /usr/bin/python
lrwxrwxrwx 1 root root 16 Feb 19 13:55 /usr/bin/python -> /usr/bin/python3
Faruk UNAL
  • 114
  • 5
  • In addition to your answer, in my case, it was needed python2. I did `sudo apt install python2` and then as you said `sudo ln -s /usr/bin/python2 /usr/bin/python` – Jafar Amini Apr 04 '22 at 07:32
2

I had the same problem. You can solve it like this:

sudo apt install python2
npm config set python "/usr/bin/python2.7"

But if you don't want to downgrade python3..

npm install -g node-gyp@latest
npm config set python "/usr/bin/python3.8"
이승현
  • 81
  • 2
  • 9