如何指定在 npm python-shell node.js 中使用哪个版本的 python
How to specify which version of python to use in npm python-shell node.js
我想使用 python-shell 在 js 文件中调用 python 脚本。但是脚本需要 python3 并且总是使用 py2,而我的本地开发同时安装了 py2 和 3。我如何指定它改为使用 python3?
app.get('/run_py', (req, res)=>{
var myPythonScriptPath = 'script.py';
// Use python shell
var PythonShell = require('python-shell');
var pyshell = new PythonShell("pyscripts/test.py");
pyshell.on('message', function (message) {
// received a message sent from the Python script (a simple "print" statement)
console.log(message);
});
// end the input stream and allow the process to exit
pyshell.end(function (err) {
if (err){
throw err;
};
//console.log('finished');
});
});
我的 py 脚本如下所示:
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
print('running test.py')
print (sys.version_info[0])
本地 运行 时总是使用 python 2.7。
此外,当我将代码推送到默认使用 python 3.6 的 heroku 服务器时。脚本运行完美...
有什么见解吗?请问?
编辑:我看到你可以在 PythonShell.run 中指定 python 路径作为参数。但是如果不同的平台说 heroku 和我的本地机器有不同的路径到 python?
根据the python-shell documentation,您可以在PythonShell
选项中指定python解释器路径。您需要做两件事:
- 确保 python3 安装在您的本地系统上(可能需要
pip install python3
或类似的东西)
- 将 python 路径更改为指向 python3(可能只需要
/usr/bin/env python3
编辑:根据您的编辑,如果您的两个节点服务在 Linux 上都是 运行,您应该可以使用 /usr/bin/env python3
。请参阅 this Whosebug question 以了解跨系统的解决方案。
更新:截至 2018 年 8 月 7 日,python3 是默认的 pythonPath。
我想使用 python-shell 在 js 文件中调用 python 脚本。但是脚本需要 python3 并且总是使用 py2,而我的本地开发同时安装了 py2 和 3。我如何指定它改为使用 python3?
app.get('/run_py', (req, res)=>{
var myPythonScriptPath = 'script.py';
// Use python shell
var PythonShell = require('python-shell');
var pyshell = new PythonShell("pyscripts/test.py");
pyshell.on('message', function (message) {
// received a message sent from the Python script (a simple "print" statement)
console.log(message);
});
// end the input stream and allow the process to exit
pyshell.end(function (err) {
if (err){
throw err;
};
//console.log('finished');
});
});
我的 py 脚本如下所示:
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
print('running test.py')
print (sys.version_info[0])
本地 运行 时总是使用 python 2.7。
此外,当我将代码推送到默认使用 python 3.6 的 heroku 服务器时。脚本运行完美...
有什么见解吗?请问?
编辑:我看到你可以在 PythonShell.run 中指定 python 路径作为参数。但是如果不同的平台说 heroku 和我的本地机器有不同的路径到 python?
根据the python-shell documentation,您可以在PythonShell
选项中指定python解释器路径。您需要做两件事:
- 确保 python3 安装在您的本地系统上(可能需要
pip install python3
或类似的东西) - 将 python 路径更改为指向 python3(可能只需要
/usr/bin/env python3
编辑:根据您的编辑,如果您的两个节点服务在 Linux 上都是 运行,您应该可以使用 /usr/bin/env python3
。请参阅 this Whosebug question 以了解跨系统的解决方案。
更新:截至 2018 年 8 月 7 日,python3 是默认的 pythonPath。