如何在 NPM 安装期间使用不同版本的 python?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20454199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a different version of python during NPM install?
提问by Nasser Torabzade
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 altinstallinstead of make install)
我可以通过终端访问运行 centos 5.9 并安装默认 python 2.4.3 的 VPS。我还通过这些命令安装了 python 2.7.3 :(我用make altinstall而不是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:
然后我通过这些命令从源代码安装了 node.js:
python2.7 ./configure
make
make install
The problem is, when I use npm installand try to install a node.js package which requires python > 2.4.3 I get this error:
问题是,当我使用npm install并尝试安装需要 python > 2.4.3 的 node.js 包时,我收到此错误:
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"?
我应该如何“通过 --python 开关指向 Python >= v2.5.0”?
采纳答案by ack
You can use --pythonoption to npm like so:
您可以--python像这样使用npm 选项:
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.
Npm 会在需要时将此选项传递给 node-gyp。
(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 ;-) )
(注意:我是在 Github 上提出问题并将其包含在文档中的人,因为关于它的问题太多了 ;-))
回答by Sandtears Kirisame
set python to python2.7 before running npm install
在运行 npm install 之前将 python 设置为 python2.7
Linux:
Linux:
export PYTHON=python2.7
Windows:
视窗:
set PYTHON=python2.7
回答by vmx
Ok, so you've found a solution already. Just wanted to share what has been useful to me so many times;
好的,您已经找到了解决方案。只是想多次分享对我有用的东西;
I have created setpy2alias which helps me switch python.
我创建了setpy2别名来帮助我切换 python。
alias setpy2="mkdir -p /tmp/bin; ln -s `which python2.7` /tmp/bin/python; export PATH=/tmp/bin:$PATH"
Execute setpy2before you run npm install. The switch stays in effect until you quit the terminal, afterwards pythonis set back to system default.
执行setpy2之前运行npm install。该开关一直有效,直到您退出终端,然后python设置回系统默认值。
You can make use of this technique for any other command/tool as well.
您也可以将此技术用于任何其他命令/工具。
回答by Dave L.
For Windows users something like this should work:
对于 Windows 用户,这样的事情应该可以工作:
PS C:\angular> npm install --python=C:\Python27\python.exe
回答by MPV
for quick one time use this works, npm install --python="c:\python27"
快速使用一次, npm install --python="c:\python27"
回答by olu mide
This one works better if you don't have the python on pathor want to specify the directory:
如果您在路径上没有 python或想要指定目录,则此方法效果更好:
//for Windows
npm config set python C:\Python27\python.exe
//for Linux
npm config set python /usr/bin/python27

