【发布时间】:2020-12-08 16:26:20
【问题描述】:
我想使用 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 路径呢?
【问题讨论】:
标签: python node.js heroku node-modules