【问题标题】:SyntaxError when firing python scirpt in nodejs在节点 js 中触发 python 脚本时出现语法错误
【发布时间】:2021-07-22 14:27:21
【问题描述】:

我正在使用 nodejs 的 python-shell 包来触发 python 脚本,该脚本返回 SyntaxError: invalid syntax。

Nodejs 代码

const options = {
    mode: 'text',
    pythonPath: '/usr/bin/python3',
    scriptPath: __dirname + '/script',
};
var pyshell = new PythonShell('test.py', options);

pyshell.send('hello');

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,code,signal) {
  if (err) throw err;
  console.log('The exit code was: ' + code);
  console.log('The exit signal was: ' + signal);
  console.log('finished');
});

Python 脚本

print(f'test')

错误

PythonShellError:   File "/app/src/back/script/test.py", line 1
print(f'meas')
            ^
SyntaxError: invalid syntax

如果我使用命令:python3 test.py 运行脚本,我会打印出“test”这个词,但不会像我想要的那样使用 python-shell。 我的python版本是3.8.10

【问题讨论】:

    标签: python node.js shell npm


    【解决方案1】:

    这正是不支持 f 字符串(3.5 或更早版本)的旧版 Python 所遇到的错误。

    尝试在您的系统上运行/usr/bin/python3,看看是什么版本。

    【讨论】:

    • 原来是我的 docker 容器,我在其上运行具有旧版本 Python 的整个东西
    猜你喜欢
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 2020-11-05
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多