簡體   English   中英

使用 node.js child_process 調用 python 腳本

[英]Call python script using node.js child_process

我試圖從我的節點文件中調用 python 代碼。

這是我的 node.js 代碼:

var util = require("util");

var spawn = require("child_process").spawn;
var process = spawn('python',["workpad.py"]);

util.log('readingin')

process.stdout.on('data',function(data){
    util.log(data);
});

和我的 python 部分:

import sys

data = "test"
print(data)
sys.stdout.flush()

在cmd window中,只顯示了util.log('readingin') 我的代碼有什么問題?

沒有問題 ...

這是你的工作代碼的輕微調整(我將緩沖區轉換為字符串,因此它的人類可讀)

// spawn_python.js
var util = require("util");

var spawn = require("child_process").spawn;
var process = spawn('python',["python_launched_from_nodejs.py"]);

util.log('readingin')

process.stdout.on('data',function(chunk){

    var textChunk = chunk.toString('utf8');// buffer to string

    util.log(textChunk);
});

這是你的python

# python_launched_from_nodejs.py
import sys

data = "this began life in python"
print(data)
sys.stdout.flush()

最后這里是一個運行的輸出

node spawn_python.js 
11 Dec 00:06:17 - readingin
11 Dec 00:06:17 - this began life in python

node --version

V5.2.0

你的python代碼不正確:

import sys

data = "test"
print(data)   ###not test
sys.stdout.flush()

我也遇到了同樣的問題,我發現了這個

var myPythonScript = "script.py";
// Provide the path of the python executable, if python is available as 
// environment variable then you can use only "python"
var pythonExecutable = "python.exe";

// Function to convert an Uint8Array to a string
var uint8arrayToString = function(data){
    return String.fromCharCode.apply(null, data);
};

const spawn = require('child_process').spawn;
const scriptExecution = spawn(pythonExecutable, [myPythonScript]);

// Handle normal output
scriptExecution.stdout.on('data', (data) => {
   console.log(uint8arrayToString(data));
});

// Handle error output
scriptExecution.stderr.on('data', (data) => {
    // As said before, convert the Uint8Array to a readable string.
    console.log(uint8arrayToString(data));
});

scriptExecution.on('exit', (code) => {
    console.log("Process quit with code : " + code);
});

你可以嘗試這樣的事情:

var child_process = require('child_process');

  child_process.exec('python myPythonScript.py', function (err){
    if (err) {
    console.log("child processes failed with error code: " + err.code);
  }
});

你可以在 npm -native-python上查看這個 package

它提供了一種非常簡單而強大的方法來從節點運行 python 函數可能會解決您的問題並實際使用 spawn。

import { runFunction } from '@guydev/native-python'
const example = async () => {
   const input = [1,[1,2,3],{'foo':'bar'}]
   const { error, data } = await runFunction('/path/to/file.py','hello_world', '/path/to/python', input)

   // error will be null if no error occured.
   if (error) {
       console.log('Error: ', error)
   }

   else {
       console.log('Success: ', data)
       // prints data or null if function has no return value
   }
}

python模塊

# module: file.py

def hello_world(a,b,c):
    print( type(a), a) 
    # <class 'int'>, 1

    print(type(b),b)
    # <class 'list'>, [1,2,3]

    print(type(c),c)
    # <class 'dict'>, {'foo':'bar'}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM