当 运行 作为 Node.js 中的子进程时 Python 版本错误?
Wrong Python version when ran as a child process in Node.js?
目前,我正在尝试 运行 一个 Python 脚本,该脚本以一串 HTML 代码响应,我正在 运行 通过 Node.js 子进程。这是我为服务器编写的代码:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
app.get('/', (req,res) => {
const spawn = require("child_process").spawn;
const pythonProcess = spawn('python',["graphing.py"]);
pythonProcess.stdout.on('data', (data) => {
console.log("It worked!")
res.write(data.toString());
res.end();
});
pythonProcess.stderr.on('data', (data) => {
console.log("ERROR!")
console.log(data.toString());
res.write("Error. Check console.");
res.end();
});
});
我已经尝试 运行 在 Python 中仅使用简单的打印语句来编译此代码,并且效果非常好。但是,当我使用正确的脚本进行尝试时,出现此错误:
Traceback (most recent call last):
File "graphing.py", line 34, in <module>
print(mpld3Plot("./data/weather/daily/Baka_ThruApr-2019.csv", ('maxTemp','minTemp')))
File "graphing.py", line 18, in mpld3Plot
with open(abs_path, newline='') as csvfile:
TypeError: 'newline' is an invalid keyword argument for this function
经过一番深入研究,我相当确定这个问题是因为服务器 运行 正在 Python 2 中的 Python 脚本,因为 .当我单独 运行 Python 脚本时,它工作得很好。我将如何让服务器具体地 运行 Python 3 中的脚本?谢谢。
编辑:
当我将路径从 'python' 更改为绝对路径时,出现此错误:
events.js:200
throw er; // Unhandled 'error' event
^
Error: spawn C:UsersNAMEAppDataLocalProgramsPythonPython38python.exe ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:81:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:270:12)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:81:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn C:UsersNAMEAppDataLocalProgramsPythonPython38python.exe',
path: 'C:UsersNAMEAppDataLocalProgramsPythonPython38python.exe',
spawnargs: [ 'graphing.py' ]
}
关于我现在应该做什么有什么想法吗?
这是从其他脚本调用 Python 时的典型问题,通常是因为从终端 运行ning 时您进入的默认环境与当前环境不同当您从程序或脚本中调用可执行文件时。因此替换:
const pythonProcess = spawn('python',["graphing.py"]);
与:
const pythonProcess = spawn('/complete/path/to/python3',["graphing.py"]);
(换句话说 - 指定您想要的确切 Python3 可执行文件的完整路径,而不是在 运行 时依赖本机环境)。
目前,我正在尝试 运行 一个 Python 脚本,该脚本以一串 HTML 代码响应,我正在 运行 通过 Node.js 子进程。这是我为服务器编写的代码:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
app.get('/', (req,res) => {
const spawn = require("child_process").spawn;
const pythonProcess = spawn('python',["graphing.py"]);
pythonProcess.stdout.on('data', (data) => {
console.log("It worked!")
res.write(data.toString());
res.end();
});
pythonProcess.stderr.on('data', (data) => {
console.log("ERROR!")
console.log(data.toString());
res.write("Error. Check console.");
res.end();
});
});
我已经尝试 运行 在 Python 中仅使用简单的打印语句来编译此代码,并且效果非常好。但是,当我使用正确的脚本进行尝试时,出现此错误:
Traceback (most recent call last):
File "graphing.py", line 34, in <module>
print(mpld3Plot("./data/weather/daily/Baka_ThruApr-2019.csv", ('maxTemp','minTemp')))
File "graphing.py", line 18, in mpld3Plot
with open(abs_path, newline='') as csvfile:
TypeError: 'newline' is an invalid keyword argument for this function
经过一番深入研究,我相当确定这个问题是因为服务器 运行 正在 Python 2 中的 Python 脚本,因为
编辑: 当我将路径从 'python' 更改为绝对路径时,出现此错误:
events.js:200
throw er; // Unhandled 'error' event
^
Error: spawn C:UsersNAMEAppDataLocalProgramsPythonPython38python.exe ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:81:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:270:12)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:81:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn C:UsersNAMEAppDataLocalProgramsPythonPython38python.exe',
path: 'C:UsersNAMEAppDataLocalProgramsPythonPython38python.exe',
spawnargs: [ 'graphing.py' ]
}
关于我现在应该做什么有什么想法吗?
这是从其他脚本调用 Python 时的典型问题,通常是因为从终端 运行ning 时您进入的默认环境与当前环境不同当您从程序或脚本中调用可执行文件时。因此替换:
const pythonProcess = spawn('python',["graphing.py"]);
与:
const pythonProcess = spawn('/complete/path/to/python3',["graphing.py"]);
(换句话说 - 指定您想要的确切 Python3 可执行文件的完整路径,而不是在 运行 时依赖本机环境)。