Integrate Python flask script with Asp.Net web form application , C#, - c#

I want to integrate the Flask script in my asp.net web application. To Achieve this there are two methods
1) Calling the Python script using the new process with installed Python interpreter.
2) Calling the Python script using IronPython interpreter, hosted in your .NET application.
if I use simple python file it executes very well. But when I run flask python script it raises an exception "Flask Module not exist".
My question is that how can we add flask and other packages at run time
or there is any best way to run python flask script.
I have tried both ways with simple python file
1) Calling the Python script using the new process with installed Python interpreter.
2) Calling the Python script using IronPython interpreter, hosted in your .NET application.
But it raises some modules exceptions
static void Main(string[] args)
{
Console.WriteLine("Execute python Process...");
Option1_ExecProcess();
Console.ReadKey();
}
static void Option1_ExecProcess()
{
// 1) Create Process Info
var psi = new ProcessStartInfo();
psi.FileName=#"C:\Users\dell\AppData\Local\Programs\Python\Python37-
32\python.exe";
// 2) Provide script and arguments
var script=#"C:\2B_Vision______________\ReadMVP\DARC_MVP.py";
var start = "2019-1-1";
var end = "2019-1-22";
psi.Arguments = $"\"{script}\" \"{start}\" \"{end}\"";
// 3) Process configuration
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
// 4) Execute process and get output
var errors = "";
var results = "";
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
results = process.StandardOutput.ReadToEnd();
}
// 5) Display output
Console.WriteLine("ERRORS:");
Console.WriteLine(errors);
Console.WriteLine();
Console.WriteLine("Results:");
Console.WriteLine(results);
}
I expect just execute the python flask script with no module issues.

Related

Getting error while running python script file from .net web application

I am trying to Execute python script from my .Net Web Application.
For this I have installed Python from nuget package manager.
Below is the Python Scrip to Insert a record in SQL Server database,
import pyodbc
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER=*****\SQLEXPRESS;DATABASE=TestDB;UID=sa;PWD=****')
cur = conn.cursor()
cur.execute("Insert into Results([EmailID],[Journey]) VALUES (?,?)", 55, "JourneyName")
conn.commit()
Print("Success")
Below is the C# Code to execute above python file.
public string run_cmd(string strPath) //this must not be async
{
try
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\Anaconda\python.exe";
start.Arguments = string.Format("C:\\Users\\261866\\TestPYCode.py");
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd();
string result = reader.ReadToEnd();
return result;
}
}
}
catch (Exception ex)
{
return ex.Message;
}
return "run till end";
}
I am getting below error while executing above code,
Traceback (most recent call last):
File "C:\Users\261866\TestPYCode.py", line 1, in <module>
import pyodbc
ModuleNotFoundError: No module named 'pyodbc'
I have tried same with Iron Python, but still getting same error.
Note: Same Python code is running in Jupiter notebook, but not from visual studio,
pip pyodbc libs are already installed
, ODBC drivers for SQL are installed
Actually same kind of error coming if I write any "import * " statement in python script.

Python code execution through .net web application : getting error "No module named pyodbc"

I am trying to Execute python script from my .Net Web Application. For this I have installed Python from nuget package manager. But I am getting following errors during execution:
I have attempted following code chunks for getting resolution but I am not able to execute it successfully.
Below is the Python Scrip to Insert a record in SQL Server database:
import pyodbc
conn = pyodbc.connect('DRIVER={SQL Server Native Client
11.0};SERVER=*****\SQLEXPRESS;DATABASE=TestDB;UID=sa;PWD=****')
cur = conn.cursor()
cur.execute("Insert into Results([EmailID],[Journey]) VALUES (?,?)", 55,
"JourneyName")
conn.commit()
Print("Success")
Below is the C# Code to execute above python file.
public string run_cmd(string strPath) //this must not be async
{
try
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\Anaconda\python.exe";
start.Arguments = string.Format("C:\\Users\\261866\\TestPYCode.py");
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd();
string result = reader.ReadToEnd();
return result;
}
}
}
catch (Exception ex)
{
return ex.Message;
}
return "run till end";
}
As I have executed above c# code I am getting below Error:
Traceback (most recent call last):"C:\Users\261866\TestPYCode.py"
import pyodbc Module Not Found Error: No module named 'pyodbc'
Error: No module named 'pyodbc'
The problem might be the source from where comes pyodbc. If your pyodbc comes from a .whl file then probably you have to install it using pip.
Here is the Microsoft's documentation says how to configure development environment for pyodbc.
Did you try it?
And here you've got ODBC Driver for SQL Server on Windows where you can download it and install.

Passing Variable Between C# And Python (IronPython?)

I am trying to pass a variable from a C# application, which is being used as an API connection.
Essentially Outsystems produces a signal, which the C# applications picks up. I then want to pass this (single integer) variable onto my python code (labelled msg in my code below), so that it can run the function using it.
I have researched, and it looks like ironpython has a solution:
{
ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);
ScriptSource source = engine.CreateScriptSourceFromFile("C:\\Scripts\\12\\Testpy\\Testpy\\Testpy.py");
ScriptScope scope = engine.CreateScope();
List<String> argv = new List<String>();
//Do some stuff and fill argv
argv.Add(msg);
engine.GetSysModule().SetVariable("argv", argv);
source.Execute(scope);
}
However, it seems to do nothing (no error, just stops executing) when it gets the the source.Execute(scope) side.
I'd imagine (?) it's because my python code isn't set up to receive variables yet. However, this is the part that I am unsure on how to write.
I - unfortunately - have little experience in passing values into python scripts, but have set up the following simple example (that is python script to python script) that seems to be working okay:
pyA
import pyB
pyB.some_func(2)
pyB
def some_func(a):
print(a)
some_func()
Alternatively, if anyone can recommend another non-Iron Python way, that would also be amazing!
Thanks in advance!! Much appreciated!
From code link in this vid: https://www.youtube.com/watch?v=g1VWGdHRkHs
var psi = new ProcessStartInfo();
psi.FileName = #"C:\PythonInstall\python.exe";
// 2) Provide script and arguments
var script = #"C:\AllTech\Code\DaysBetweenDates.py";
var start = "2019-1-1";
var end = "2019-1-22";
psi.Arguments = $"\"{script}\" \"{start}\" \"{end}\"";
// 3) Process configuration
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
// 4) Execute process and get output
var errors = "";
var results = "";
using(var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
results = process.StandardOutput.ReadToEnd();
}
// 5) Display output
Console.WriteLine("ERRORS:");
Console.WriteLine(errors);
Console.WriteLine();
Console.WriteLine("Results:");
Console.WriteLine(results);

Call a python script with parser arguments in .NET

Sorry if this is a duplicate question but I could not find clear answer.
I am trying to call a python script (dnstwist.py) from within .NET using IronPython.
public static void runDNSTwistPython(string url)
{
var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile("dnstwist-master\\dnstwist.py");
test.main();
}
I need a way to call this and put the output in a .csv file. I can easily do this from command line but can't figure out how to do it in my C# method.
python dnstwist.py --csv google.com > output.csv
Thanks
#jacob-hall you can use C# Process class and pass the command line to be executed this class documentation can be found here Process
and you code will be something like this:
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "python dnstwist.py --csv google.com > output.csv";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();

Execute Python From C# Without IronPython [duplicate]

This sort of question has been asked before in varying degrees, but I feel it has not been answered in a concise way and so I ask it again.
I want to run a script in Python. Let's say it's this:
if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
s = f.read()
print s
Which gets a file location, reads it, then prints its contents. Not so complicated.
Okay, so how do I run this in C#?
This is what I have now:
private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = cmd;
start.Arguments = args;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
When I pass the code.py location as cmd and the filename location as args it doesn't work. I was told I should pass python.exe as the cmd, and then code.py filename as the args.
I have been looking for a while now and can only find people suggesting to use IronPython or such. But there must be a way to call a Python script from C#.
Some clarification:
I need to run it from C#, I need to capture the output, and I can't use IronPython or anything else. Whatever hack you have will be fine.
P.S.: The actual Python code I'm running is much more complex than this, and it returns output which I need in C#, and the C# code will be constantly calling the Python code.
Pretend this is my code:
private void get_vals()
{
for (int i = 0; i < 100; i++)
{
run_cmd("code.py", i);
}
}
The reason it isn't working is because you have UseShellExecute = false.
If you don't use the shell, you will have to supply the complete path to the python executable as FileName, and build the Arguments string to supply both your script and the file you want to read.
Also note, that you can't RedirectStandardOutput unless UseShellExecute = false.
I'm not quite sure how the argument string should be formatted for python, but you will need something like this:
private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "my/full/path/to/python.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using(Process process = Process.Start(start))
{
using(StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
If you're willing to use IronPython, you can execute scripts directly in C#:
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
private static void doPython()
{
ScriptEngine engine = Python.CreateEngine();
engine.ExecuteFile(#"test.py");
}
Get IronPython here.
Execute Python script from C
Create a C# project and write the following code.
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
run_cmd();
}
private void run_cmd()
{
string fileName = #"C:\sample_script.py";
Process p = new Process();
p.StartInfo = new ProcessStartInfo(#"C:\Python27\python.exe", fileName)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
}
}
}
Python sample_script
print "Python C# Test"
You will see the 'Python C# Test' in the console of C#.
I ran into the same problem and Master Morality's answer didn't do it for me. The following, which is based on the previous answer, worked:
private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = cmd;//cmd is full path to python.exe
start.Arguments = args;//args is path to .py file and any cmd line args
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using(Process process = Process.Start(start))
{
using(StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
As an example, cmd would be #C:/Python26/python.exe and args would be C://Python26//test.py 100 if you wanted to execute test.py with cmd line argument 100. Note that the path the .py file does not have the # symbol.
Actually its pretty easy to make integration between Csharp (VS) and Python with IronPython. It's not that much complex... As Chris Dunaway already said in answer section I started to build this inegration for my own project. N its pretty simple.
Just follow these steps N you will get your results.
step 1 : Open VS and create new empty ConsoleApp project.
step 2 : Go to tools --> NuGet Package Manager --> Package Manager Console.
step 3 : After this open this link in your browser and copy the NuGet Command.
Link: https://www.nuget.org/packages/IronPython/2.7.9
step 4 : After opening the above link copy the PM>Install-Package IronPython -Version 2.7.9
command and paste it in NuGet Console in VS.
It will install the supportive packages.
step 5 : This is my code that I have used to run a .py file stored in my Python.exe
directory.
using IronPython.Hosting;//for DLHE
using Microsoft.Scripting.Hosting;//provides scripting abilities comparable to batch files
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
class Hi
{
private static void Main(string []args)
{
Process process = new Process(); //to make a process call
ScriptEngine engine = Python.CreateEngine(); //For Engine to initiate the script
engine.ExecuteFile(#"C:\Users\daulmalik\AppData\Local\Programs\Python\Python37\p1.py");//Path of my .py file that I would like to see running in console after running my .cs file from VS.//process.StandardInput.Flush();
process.StandardInput.Close();//to close
process.WaitForExit();//to hold the process i.e. cmd screen as output
}
}
step 6 : save and execute the code
Set WorkingDirectory or specify the full path of the python script in the Argument
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\\Python27\\python.exe";
//start.WorkingDirectory = #"D:\script";
start.Arguments = string.Format("D:\\script\\test.py -a {0} -b {1} ", "some param", "some other param");
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
I am having problems with stdin/stout - when payload size exceeds several kilobytes it hangs. I need to call Python functions not only with some short arguments, but with a custom payload that could be big.
A while ago, I wrote a virtual actor library that allows to distribute task on different machines via Redis. To call Python code, I added functionality to listen for messages from Python, process them and return results back to .NET.
Here is a brief description of how it works.
It works on a single machine as well, but requires a Redis instance. Redis adds some reliability guarantees - payload is stored until a worked acknowledges completion. If a worked dies, the payload is returned to a job queue and then is reprocessed by another worker.
had same issure and this worked for me:
using IronPython.Hosting;
var engine = Python.CreateEngine();
engine.ExecuteFile("") //put the directory of the program in the quote marks
I had the same issue and used answers on here to solve it using a process. I had conflicts between my .NET and IronPython so wasn't successful there. This works well with my python 3.10.
public void Run_cmd2(string exe, string args, string output )
{
var outputStream = new StreamWriter(output);
// create a process with the name provided by the 'exe' variable
Process cmd = new Process();
cmd.StartInfo.FileName = exe;
//define you preference on the window and input/output
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
// write the output to file created
cmd.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
outputStream.WriteLine(e.Data);
}
});
cmd.Start();
// write to the console you opened. In this case for example the python console
cmd.StandardInput.WriteLine(args);
//Read the output and close everything. make sure you wait till the end of the process
cmd.BeginOutputReadLine();
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
//close the process. writing to debug helps when coding
outputStream.Close();
//Console.WriteLine(cmd.StandardOutput.ReadToEnd());
cmd.Close();
Debug.WriteLine("\n\n Process done!");
//Console.ReadLine();
}
Example call:
string pythonEngine = "C:\ProgramData\Anaconda3\envs\compVision\python.exe";
string pythonArguements = "import os ; os.chdir('C:\YourPath\excelWorkbooks') ; import testSearch ; testSearch.performAdd(2, 3)";
// here a function in testSearch.py is called. To run the .py directly do this:
string pythonArguements = "import os ; os.chdir('C:\YourPath\excelWorkbooks') ; import testSearch ; testSearch.py";
outFile = "C:\YourPath\output.txt";
_yourModuleName.Run_cmd2(pythonEngine, pythonArguements, outFile);

Categories

Resources