I want to run the following python script using C# and MonoDevelop and execute the code.
My Mono Develop code
string command = "python";
string argss = "/home/xyz/script.py A_file B_file";
string verb = "";
ProcessStartInfo procInfo = new ProcessStartInfo();
procInfo.WindowStyle = ProcessWindowStyle.Normal;
procInfo.UseShellExecute = false;
procInfo.FileName = command;
procInfo.Arguments = argss;
procInfo.Verb = verb;
Process.Start(procInfo);
The above code is not working. It is showing A_file & B_File are not accessible.
even A_File & B_File has been give full permission & file is placed at same location.
python script is working correctly when compile directly on terminal.
I am running MonoDevelop on Ubuntu
Related
I have anaconda3 installed on my Windows 10 machine.
I want to get the python script to run and return the results to C#.
So here is my code in C#:
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\Users\John\AppData\Local\Cotinum\anaconda3\python.exe";
start.Arguments = string.Format("{0}", #"C:\test.py", "test");
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using(Process process = Process.Start(start))
using (StreamReader reader = process.StandardOutput)
{
string foo = reader.ReadToEnd();
TxtResultOutput.Text += foo;
}
The python code
import pandas as pd
import sys
def test_func(f_name):
a = pd.read_excel(f_name)
return a
if __name__ == "__main__":
f_name = sys.argv[1]
test_func(f_name)
When I run C# code, in the output window of Visual Studio. It shows the error:
File "C:\Users\John\AppData\Local\Continuum\anaconda3\lib\site-package\pandas__init__.py", line 19,
in module
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
However I don't believe I missed it because I can run the script from anaconda3 prompt window. It must be that somehow the PATH can't found in Visual Studio/ C# environment.
I have to run a JS script in a specific folder. The script I use requires this step to be run. I run this in cmd:
D:\xxx> c:\Windows\SysWOW64\cscript.exe script.js
and it works fine. But when I try running this command in my .NET Core app, the script runs, but does not work properly. I'm using this method:
Directory.SetCurrentDirectory(#"D:\xxx");
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = #"C:\Windows\SysWOW64\cscript.exe";
scriptProc.StartInfo.WorkingDirectory = scriptDir;
scriptProc.StartInfo.Arguments = #"script.js";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
scriptProc.Start();
scriptProc.WaitForExit();
int result = scriptProc.ExitCode;
scriptProc.Close();
I also create a .bat file runing this script. When i open it in manually it work fine but when i try to open it in my app the script runs but does not work properly. To run .bat file i am using this method:
ProcessStartInfo info = new ProcessStartInfo(batFilePath);
info.UseShellExecute = true;
Process.Start(info);
What am I doing wrong?
I run script properly when I add
scriptProc.StartInfo.UseShellExecute = false;
scriptProc.StartInfo.Domain = myDomain;
scriptProc.StartInfo.UserName = myUserName;
string password1 = myPassword;
for (int x = 0; x < password1.Length; x++)
{
ssPwd.AppendChar(password1[x]);
}
password1 = "";
scriptProc.StartInfo.Password = ssPwd;
I followed this_link and I was able to run a dummy python file from my c# code like this...
public JsonResult FetchscrapyDataUrl(String website)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\ProgramData\Anaconda3\python.exe";
start.Arguments = #"C:\Users\PycharmProjects\scraping_web\scrape_info\main.py";
//this is path to .py file from scrapy project
start.CreateNoWindow = false; // We don't need new window
start.UseShellExecute = false; // Do not use OS shell
//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)
Console.WriteLine("Python Starting");
start.RedirectStandardOutput = true;
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")
Console.Write(result);
}
}
}
Now I know that I can run scrapy spider from a single file main.py like this...
from scrapy import cmdline
cmdline.execute("scrapy crawl text".split())
When I run main.py file from cmd in windows it works fine but it does not work when I run it from C# code .Net framework. The error is ...
"Scrapy 1.4.0 - no active project\r\n\r\nUnknown command: crawl\r\n\r\nUse \"scrapy\" to see available commands\r\n"
Any Idea how to run this...Or am i missing some path setting in windows ??
Or should I run my spider from C# in some other way??
You need to set the WorkingDirectory property
start.WorkingDirectory = #"C:\Users\PycharmProjects\scraping_web\scrape_info\"
Or you need to cd to that directory to make it work
I eventually want to be able to return a DataFrame from Python to C#. At the moment I am running a very simple console app to try and run a basic Python script. This script compiles and runs in Canopy fine however when I run it from C# I get the error relating to non-ASCII chars.
I have read many articles relating to this but none of them seem to resolve the issue I have.
Error
SyntaxError: Non-ASCII character '\x90' in file C:\Program Files\Enthought\Canop
y32\App\appdata\canopy-1.5.2.2785.win-x86\python.exe on line 1, but no encoding
declared; see http://www.python.org/peps/pep-0263.html for details
Thx in advance for any help!
static int test_python_canopy()
{
string cmd;
string args;
args = "C:\\Share\\Python\\test.py";
cmd = #"C:\Program Files\Enthought\Canopy32\App\appdata\canopy-1.5.2.2785.win-x86\python.exe";
cmd = "\"" + cmd + "\"";
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = cmd;
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);
}
}
return 0;
}
For ProcessStartInfo, FileName should be set to the executable that you wish to run and Arguments should be set to the arguments that you want to pass to that executable.
In your code, FileName is set correctly to the Python interpreter. Arguments, however, the first argument is being set to Python interpreter. The net result is that C# is trying to execute this command:
C:\Program Files\Enthought\Canopy32\App\appdata\canopy-1.5.2.2785.win-x86\python.exe C:\Program Files\Enthought\Canopy32\App\appdata\canopy-1.5.2.2785.win-x86\python.exe C:\Share\Python\test.py
which means that Python is trying to use the Python executable as a script, which is not going to work. Changing the line that sets the arguments should fix the problem:
start.Arguments = args;
I'm calling FFmpeg via ProcessStartInfo inside my C# application however, I can't keep getting the error;
File for preset 'lossless_slow' not
found
Here's my C# code;
var processinfo = new ProcessStartInfo();
processinfo.FileName = "FFmpeg\\bin\\ffmpeg.exe";
processinfo.Arguments = "-i C:\Temp\input.mp4 -y -acodec aac -strict experimental -ab 96k -vcodec libx264 -vpre lossless_slow -crf 22 -threads 0 C:\Temp\output.mp4"
processinfo.RedirectStandardOutput = true;
processinfo.RedirectStandardError = true;
processinfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
processinfo.UseShellExecute = false;
processinfo.LoadUserProfile = true;
processinfo.EnvironmentVariables.Add("HOME", #"C:\Users\wonea\.ffmpeg");
var reg = System.Diagnostics.Process.Start(processinfo);
string output = string.Empty;
string error = string.Empty;
using (System.IO.StreamReader myOutput = reg.StandardOutput)
{
output = myOutput.ReadToEnd();
}
using (System.IO.StreamReader myError = reg.StandardError)
{
error = myError.ReadToEnd();
}
Now I've put my presets in the folder
C:\Users\wonea\ .ffmpeg
and included this in the Windows path user variable HOME. This works fine when running FFmpeg from the command line, however fails when the commands are issued inside my C# application, why!? Thanks for any help...!
Also of note, I'm running the service as "Network Service".
In situations like this I always start up the procmon tool which can show you all the file operations of your application. You can set up a filter based on the name of the preset file and see where ffmpeg is trying to locate it.
Setting the HOME variable looks good here.
The only thing I see is that in the code you set HOME to be #"C:\Users\wonea.ffmpeg" and in the text you mention the file is at : C:\Users\wonea.ffmpeg
Is one of these a typo?