Can't read a file in python using node - python

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});

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?

Node.js spawn module does not work with 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()

Permission denied when trying to execute python process from DotNet Core MVC project

I've created a DotNet Core Razor MVC project and in there, I have created a directory called python. Within that python directory, I have created a python script called myscript.py.
Following the real-time chat example for SignalR, I have added a function to my ChatHub which is supposed to execute myscript.py:
private string ProcessMessage(string message)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "/Users/SomeUser/Documents/Code/VisualStudio/MyProject/MyProject/python/myscript.py";
start.Arguments = message;
start.UseShellExecute = false;// Do not use OS shell
start.CreateNoWindow = true; // We don't need new window
start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
return result;
}
}
}
Notice I have provided the full path to start.FileName. However, when I run this, I get a permission denied error.
I've tried various combinations of that path (e.g. ~/python/myscript.py)
How can I resolve this? I'm running VS for Mac, if that makes a difference.

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 spawn result: python3: can't open file './test': [Errno 2] No such file or directory

So I'm trying to run a simple python file called test.py through the Node JS child process, however it keeps saying that python3: can't open file './test': [Errno 2] No such file or directory. I tried everything to fix this error. For example, the python script ran successfully from the same directory the typescript file is present, I even tried the full path, the relative path for the python file and it still didn't work. I kindly request someone too look into this, thank you!
test.py
import sys
print("hello world")
sys.stdout.flush()
var spawn = require("child_process").spawn;
var child = await spawn("python3", ['./test.py']);
let dataMain = "";
child.stdout.on("data", (data: Buffer) => {
// Do something with the data returned from python script
console.log(data); // buffer data
dataMain += data.toString();
});
child.stdout.on("end", () => {
console.log("end");
console.log(dataMain);
});
child.stderr.pipe(process.stderr); // prints out the error
Worked with ${process.cwd()}/test.py

Categories