1 'test.py' py文件路径
2 options 对象/字典, 等于null时 js不向py传参,只接收参数
3 js 代码
const { PythonShell } = require('python-shell');
// 创建要传递给Python脚本的数组
const myArray = [1, 2, 3, 4, 5];
// 配置PythonShell选项
const options = {
args: myArray
};
// 运行Python脚本,并处理结果
PythonShell.run('test.py', options, function (err, result) {
if (err) throw err;
// 处理Python脚本的输出结果
console.log(result);
});
4 python代码
import sys
# 获取传递的参数
args = sys.argv[1:]
# 打印参数
print(args)