找不到Python可执行文件"python

47 投票
7 回答
101006 浏览
提问于 2025-04-18 06:01

我在用 npm 安装 iconv 的时候,遇到了以下错误:

iconv@2.1.0 安装在 /root/Dropbox/nodeApps/nodeApp/node_modules/iconv node-gyp 重建

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

虽然我已经安装了 Python,并且可以在控制台运行它:

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

并且在 ~/.bashrc 中设置了 PATH

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

还执行了 . ~/.bashrc

7 个回答

3

我也遇到过同样的问题。你可以这样解决:

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

不过,如果你不想把python3降级的话……

npm install -g node-gyp@latest
npm config set python "/usr/bin/python3.8"
8

安装 Python 2.7。
apt install python2

11

这里有一个简单又安全的方法。

把下面的内容放到 ~/.bashrc 或者 ~/.bash_aliases 文件里:

alias python=python3

添加完这些内容后,运行 source ~/.bashrc 或者 source ~/.bash_aliases

这个方法在我的Ubuntu上有效,想了解更多可以查看原始回答 这里

11

在你的bash会话中,如果你可以直接输入 python 并得到有效的响应,那么输入 which python,记下 python 程序的完整路径。然后把这个路径放到你的 PYTHONPATHPATH 环境变量中,但要注意最后不要加上 python

比如,输入 which python 后我得到了:

/usr/local/bin/python

那么我会在我的 ~/.bashrc 文件中写:

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

对于在Ubuntu 16.04上遇到这个问题的朋友们...
node-gyp 不能使用Python 3.5.X,而这个版本似乎是16.04自带的默认版本。我在某个地方看到过说16.04应该也会自带Python2,但我在我的安装中找不到它。

我通过以下方法解决了这个问题:

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

现在,当 node-gyp 去寻找 python 时,它会找到你安装的Python2.7,并正确加载。

撰写回答