Run python script with ML into C# console app error - python

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#.

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.

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

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