NLTK and stopwords Lookuperror in C# application - c#

I am running a python script from a C# application. The script runs fine on command prompt/terminal but fails to execute when invoked via C# code.
It says Resource u'corpora/stopwords' not found. Please use the NLTK Downloader to obtain the resource: >>> nltk.download() even though I have all the required data/stopwords
Below is the error report from debug tab in Visual Studios.
Traceback (most recent call last):
File "C:\Users\Amey\Anaconda3\envs\dato-env\TrainingSetsUtil.py", line 20, in <module>
stopwords = set(stopwords.words('english'))
File "C:\Users\Amey\Anaconda3\envs\dato-env\lib\site-packages\nltk\corpus\util.py", line 99, in __getattr__
self.__load()
File "C:\Users\Amey\Anaconda3\envs\dato-env\lib\site-packages\nltk\corpus\util.py", line 64, in __load
except LookupError: raise e
LookupError:
**********************************************************************
Resource u'corpora/stopwords' not found. Please use the NLTK
Downloader to obtain the resource: >>> nltk.download()
Searched in:
- 'nltk_data'
**********************************************************************
Here's the invoking code.
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\Users\Amey\Anaconda3\envs\dato-env\python.exe";
start.Arguments = #"C:\Users\Amey\Anaconda3\envs\dato-env\TrainingSetsUtil.py " + uname;
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")
Console.WriteLine(result);
Console.WriteLine(stderr);
}
}

Related

execute Sqlite3 command line tool from code

I'm trying to run the sqlite.exe tool as a process in my c# code in order to read a sql from a file.
If I run the sqLite3 tool in powershell then it works fine (sqlite3.exe "mydatabase.db" ".read mySql.sql")
But when I run the sqlite3 tool from my c# code as a process, then nothing happens to mydatabase.db. It's still 0b when sqlite3 terminates.
I get no error message, the output from the sqlite3.exe is an empty string and the exit code is 1 (verified in the exit event). Does anyone have a clue why the database.db why the records in the .sql file is not added to the .db file?.
using (Process pProcess = new Process())
{
pProcess.StartInfo.FileName = sqlLite3ExePath;
pProcess.StartInfo.Arguments = $"\"{sqLitePath2}\" \".read {sqlPath}\"";;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;//System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = false;//true; //not diplay a windows
pProcess.EnableRaisingEvents = true;
pProcess.Exited += PProcess_Exited;
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
Debug.WriteLine(output);
}
As CaiusJard said in the comments, an error was passed in the error stream. Adding the following lines told me that my path was wrong.
pProcess.StartInfo.RedirectStandardError = true;
string error = pProcess.StandardError.ReadToEnd(); //The output result
The path divider "\" was removed since the path was parsed twice. Once setting the argument, and once when it was read by the tool. Replacing "\" with "/" in my paths did the trick

python pandas missing required dependencies 'numpy' in visual studio

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.

Exception "The requested operation requires elevation" when i run my .net code

"I'm trying to execute "main.py" file which is written in Python by my .net core web API but i got an exception.
I already give the permission of my Web API folder as well as my Python Code folder.
var file = Configuration.GetValue<string>("DE.PythonPath");
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = file;
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
string stderr = process.StandardError.ReadToEnd();
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
return result;
}
}
I expect the python code run but it gives exception "The requested operation requires elevation"
Visual studio need to be run as admin. So run the Visual studio as Run as Administrator

Unable to run python script in c# which runs scrapy spider

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

Using C# 4.0 to run Python scripts error non-ascii char

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;

Categories