How to bundle python code with vsix (vscode extension) - python

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?

Related

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

C#-Pandas Process Error (.NET): no module named pandas

I have a simple GUI with two buttons and a rich text box. Button One uses the OpenFileDialog() class to fetch a video file and write the path as a string in the rich text box. Button 2 takes the value of the rich text box and sends it file to a Python script. This part works fine. Here is the function for sending the file.
I am operating out of Visual Studio 19.
C# .NET framework
public void PotatoOne(string path)
{
var neat = "r'" + path;
var shell = "python.exe";
Process x = new Process();
x.StartInfo = new ProcessStartInfo()
{
FileName = shell,
Arguments = ($"\"{"C:\\Users\\BeckerLab\\source\\repos\\Final Implementation\\Final Implementation\\untitled7.py"}\" \"{neat}\""),
ErrorDialog = true,
WorkingDirectory = Path.GetDirectoryName(#"C:\ProgramData\Anaconda3\envs\tensorflow1"),
UseShellExecute = false,
CreateNoWindow = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
try
{
x.Start();
var res = x.StandardOutput.ReadToEnd();
var err = x.StandardError.ReadToEnd();
Console.WriteLine(res);
Console.WriteLine();
Console.WriteLine(err);
Console.WriteLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadKey();
}
BUT the Process says I do not have some of the modules. Here they all are:
(notably, os, sys, numpy and cv2 are all understood. pandas and PIL, are not)
Python Code, (attempted Anaconda3, tensorflow1 envs)
import os
import cv2
import numpy as np
import sys
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import PIL
path = sys.argv[1]
I have untitled7.py in its own folder as a python file with its own solution, and they are both using the same Python environments, with the same module patches. Furthermore, untitled7.py has access to all the imports in the regular Python script, but not the .NET process.
It is clear that whatever I am doing, it's not calling the python file to run the same way. Is it because I am calling the Process incorrectly? Do I need to call a virtual environemt? Why can't I call the actual Python environment to run a Python script? Why does the exact same environment not provide the same set of modules for both solutions?
Thank you for your time.

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