找不到Python可执行文件“python”

问题描述 投票:0回答:8

当我使用

iconv
安装
npm
时出现以下错误:

[电子邮件受保护] 安装 /root/Dropbox/nodeApps/nodeApp/node_modules/iconv 节点 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! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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

并在

PATH
中设置
~/.bashrc
:

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

完成了

. ~/.bashrc

python node.js npm ubuntu-server
8个回答
103
投票

对于在 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 安装并正确加载。


11
投票

在 bash 会话中,您只需输入

python
即可获得有效响应,输入
which python
并记下
python
二进制文件的完整路径位置。获取该位置并将其放入您的
PYTHONPATH
PATH
环境变量中,除非末尾没有
python

例如,

which python
给我:

/usr/local/bin/python

所以我会写:

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

在我的

~/.bashrc


11
投票

有一个简单又安全的方法 将其放入

~/.bashrc
~/.bash_aliases
文件中:

alias python=python3

在文件中添加上述内容后,运行

source ~/.bashrc
source ~/.bash_aliases

这个解决方案适用于我的 Ubuntu 请参阅原始答案这里


7
投票

安装python2.7。

apt install python2


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"

2
投票

问题是因为 ssh 登录时未加载

~/.bashrc
。我把
PATH
变量放到
~/.bash_profile
就可以了


2
投票

有时候不仅仅和NPM有关。因此,需要一个系统范围的解决方案。然后,只需为“python”创建一个系统范围的符号链接到当前的 python 路径。

例如,对于我的 Ubuntu 20 设置(root):

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

然后验证符号链接:

ls -l /usr/bin/python
lrwxrwxrwx 1 root root 16 Feb 19 13:55 /usr/bin/python -> /usr/bin/python3

0
投票
echo "alias python=python3" >>  ~/.zshrc

source ~/.zshrc
© www.soinside.com 2019 - 2024. All rights reserved.