Node.js spawn module does not work with python - python

Currently I have a very basic js and python script.
The problem is that the python text is not displayed after running the JS file in the CMD.
I am working in Windows 10 OS.
Does someone see what I am doing wrong?
var spawn = require('child_process').spawn;
var child = spawn('python3', ['hello.py']);
child.stdout.on('data', (data) => {
console.log(data.toString());
});
-->
Python Code for testing:
import sys
data = "hallo hallo test"
print(data)
sys.stdout.flush()

Related

How to bundle python code with vsix (vscode extension)

I am creating a vscode extension where I need machine learning tasks to be performed. I have python files that have code that is required in vscode extension. I don't want things to be done using request-response on any python server. What I want is to perform the ML tasks on device (integrated with the vsix).
We have child-process available in js to run basic python file using spawn. It is running fine on both, extension host window and external vscode editor after packaging, with the python code that has basic imports like import sys. But if I try to import some other libraries like numpy, pygments, it is working only on extension host environment, not on other vs window after packaging it. How can I run the typical python code with the vsix?
Below are both the codes that is working fine and not working at all-
TS file (MLOps.ts)-
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { join } from "path";
import * as vscode from 'vscode'
export async function pythonOps(): Promise<string> {
var result = "testt"
var promise = await new Promise((resolve, reject) => {
var p = __dirname.split('\\')
p.pop()
var path = p.join('\\')
var pyPath = join(path, 'src', 'py_operations.py')
var result = "blank result"
var arg1 = "arg one"
var arg2 = "arg two"
var py_process = spawn('python', [pyPath, arg1, arg2])
py_process.stdout.on('data', (data: any) => {
vscode.window.showInformationMessage(data.toString())
result = data.toString()
})
})
}
Working Python code (py_operations.py)- This code is working on both, on extension host window and after packaging the extension and installing the vsix on other system.
import sys
print("Some text with: ",sys.argv[0], sys.argv[1], sys.argv[2])
sys.stdout.flush()
Not working Python code- This code is working only on extension host window, and not working after packaging this and isntalling on other system.
import sys
from pygments.lexers.javascript import TypeScriptLexer
lexer = TypeScriptLexer()
src = "alert('text here')"
lexer_tokens = lexer.get_tokens(src)
l = []
for t in lexer_tokens:
l.append(t[1])
print("list: ",l)
sys.stdout.flush()
How can I run the second python code with packaged vsix?

Run python script with ML into C# console app error

I wanna run in C# console app python script with libraries like numpy, pandas, matplotlib.pyplot. If I run simple print('hello world') and save it as test.py it works.
I added to PATH : D:\ProgramData\Anaconda3\Library\bin but it didn't help me too. Thanks for try helping me!
Code:
// full path to .py file
string pyScriptPath = #"C:\Users\micha\Documents\ML\multipleLinearRegression\multiple_linear_regression.py";
string outputString = null;
// create new process start info
ProcessStartInfo prcStartInfo = new ProcessStartInfo
{
// full path of the Python interpreter 'python.exe'
FileName = #"D:\ProgramData\Anaconda3\python.exe", // string.Format(#"""{0}""", "python.exe"),
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = false,
Arguments = #"C:\Users\micha\Documents\ML\multipleLinearRegression\multiple_linear_regression.py"
};
// start process
using (Process process = Process.Start(prcStartInfo))
{
// read standard output JSON string
using (StreamReader myStreamReader = process.StandardOutput)
{
outputString = myStreamReader.ReadLine();
process.WaitForExit();
}
}
Console.WriteLine(outputString);
console error
You said it works when you run the script with Python. Are you first creating an environment before running it? That may be the issue why it doesn't work when running it from C#.

Node.JS Pyshell crashes on second message to Python, [ERR_STREAM_WRITE_AFTER_END]: write after end

I have a Node.JS server that sends messages to a Python script using Pyshell.send
When Pyshell returns a message using print Node.JS recieves it using pyshell.on and then reads out the returned message.
When this is then done again with a second message it crashes with the error:
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:243:12)
at Socket.Writable.write (_stream_writable.js:291:5)
at PythonShell.send (/Users/maxkenney/Desktop/nodeLanguageProcessing/node_modules/python-shell/index.js:285:20)
at Namespace.<anonymous> (/Users/maxkenney/Desktop/nodeLanguageProcessing/app.js:87:10)
at Namespace.emit (events.js:189:13)
at Namespace.emit (/Users/maxkenney/Desktop/nodeLanguageProcessing/node_modules/socket.io/lib/namespace.js:213:10)
at /Users/maxkenney/Desktop/nodeLanguageProcessing/node_modules/socket.io/lib/namespace.js:181:14
at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
at writeAfterEnd (_stream_writable.js:245:10)
at Socket.Writable.write (_stream_writable.js:291:5)
[... lines matching original stack trace ...]
at process._tickCallback (internal/process/next_tick.js:61:11)
I believe it could possibly be because the Python shell is closed after one message but I am unsure. Any help would be greatly appreciated!
Node.JS Script (relevant parts)
let pyshell = new PythonShell('my_script.py');
pyshell.on('message', function(message){
console.log("python said: " + message);
});
pyshell.send("This is a test entry").end(function(err){
if(err) handleError(err);
});
That last pyshell.send is repeated later on and it crashes.
Python Script:
import sys
import spacy
import nltk
nlp = spacy.load("en_core_web_sm");
def convertGeneratorObjectToListOfString(generatorObject):
list = []
for item in generatorObject:
list.append(str(item))
return list
def main():
#doc = nlp(sys.argv[1])
doc = nlp(sys.stdin.readline())
# Return the printed message to Node
jsonObjectToReturn = {}
for token in doc:
dependents = token.children
listOfChildrenTag = []
for d in dependents:
listOfChildrenTag.append(d.dep_)
jsonObjectToReturn[token.text] = {
"text": token.text,
"lemma": token.lemma_,
"tag": token.tag_,
"dep": token.dep_,
"dependencies": convertGeneratorObjectToListOfString(token.children),
"dependenciesTag": listOfChildrenTag
}
print(jsonObjectToReturn)
if __name__ == '__main__':
main()
I have included all python code here as I am unsure but I believe the issue could also be caused by the fact that the Python script closes once the main finishes. Apologies I am unfamiliar with running constant python scripts.
When you call .end() you close the stream so it cannot be used any more. So, don't call .end() until you're done with the stream. You can call .send() without calling .end().
if you also need to monitor when the python script ends, you can use the childProcess property of the pyshell object.
let pyshell = new PythonShell('my_script.py');
pyshell.childProcess.on('exit', (code) => {
console.log(`python program ended with exit code: ${code}`);
});

Run a python script with NodeJS causing problems

My python script has two main steps:
Open two webbrowser tabs (default browser). And let a LED blink.
When i am executing the python script via shell with "python netflix.py" everything works fine.
But when i am trying to start it via my (see below) NodeJS script. Only the LED will be blink. The webbrowser tabs won't come up.
Does anyone know where the issue is?
#!/usr/bin/env node
process.env.UV_THREADPOOL_SIZE = 1208;
var http = require('http');
const server = http.createServer(function(request, response) {
try{
console.log('Request incoming...');
var spawn = require('child_process').spawn;
if(request.url == '/abort'){
console.log('Calling abort.py ...');
var process = spawn('python',["./abort.py"]);
}else{
console.log('Calling netflix.py');
var process = spawn('python',["./netflix.py"]);
}
}catch(e){
console.log('Error');
console.log(e);
}
});
server.on('error', function(){
console.log('error');
});
const port = 8000;
server.listen(port);
server.timeout = 10000;
console.log(`Listening at http://leitner-pi:${port}`);
import sys
import webbrowser
from gpiozero import LED
from time import sleep
try:
webbrowser.open_new_tab("http://netflix.com");
finally:
print("")
print("Lasse PIN 7 blinken..")
led = LED(17)
while True:
led.on()
sleep(0.3)
led.off()
sleep(0.3)
Fixed: Well, I made a workaround.
First, I split the python script into two scripts. Webbrowser and LED script, to avoid any kind of interruptions or something else. Second, I changed the webbrowser to:
os.system(‘sudo -upi chromium-browser URL1 URL2’).
At last, I am calling both new
scripts from my node webserver, like I used before.
Now it works fine.

Can't read a file in python using node

I'm running python in a node web app, and I'm trying to load and read a file in python, do something with it, then spit it out to node.js.
When I run the following python code, nothing happens.
Python
import json
import sys
with open('trainingData.json') as file:
data = json.load(file)
print(data)
print('hello from python')
sys.stdout.flush()
When I remove with open, then it works well. How can I read a file in python and call that file in node.js? Here's the node code
Node
app.get('/', (req, res) => {
const spawn = require('child_process').spawn;
const process = spawn('python', ['./python/script.py', 'Hello', 'World']);
process.stdout.on('data', data => console.log(data.toString()));
res.send('he');
});
(When I run the python file fro the terminal, it works correctly.)
You can use spawn's cwd (current working directory) option, to specify the directory. To set it to the "current" current working directory use __dirname.
const process = spawn('python', ['./python/script.py', 'Hello', 'World'], {cwd: __dirname});

Categories