-2

I'm working on a MVC.net website in which I use python script in it. I have a python file which I call it in order to get some results from it. The algorithm has been wrtiten in python and I have to call to get the result. If I want to execute python file, I have to call python.exe file which I call it like below and the result of calling file is string. When I run website locally it works fine but now I want to publish website to server and don't know how to upload python file in it. Second question is that how should I change the path of a python.exe and python file to run on cpanel.

string python = @"C:\Users\Cebit\AppData\Local\Programs\Python\Python37\python.exe";
string myPythonApp = @"C:\Users\Cebit\Desktop\phase2-final.py";

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);          
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.CreateNoWindow = true;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardError = true;

List<String> argv = new List<String>();
argv.Add(d1);
argv.Add(m1);
argv.Add(y1);
argv.Add(h1);
argv.Add(min1);
argv.Add(d2);
argv.Add(m2);
argv.Add(y2);
argv.Add(h2);
argv.Add(min2);            
argv.Add(favor);
argv.Add(budget.ToString());

myProcessStartInfo.Arguments = string.Format("{0} {1} {2} {3} {4} {5} {6}      {7} {8} {9} {10} {11} {12}", myPythonApp, argv[0], argv[1], argv[2], argv[3],   argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]); 

Process myProcess = new Process();
myProcess.StartInfo = myProcessStartInfo;         
myProcess.Start();           
StreamReader myStreamReader = myProcess.StandardOutput;           
myProcess.WaitForExit();           
string myString = myStreamReader.ReadToEnd();
myProcess.Close();

My question is how should I publish a website which I have to run python.exe and python file in a server. Should I have to do something extra in order to run python.exe file in server ? I'm new to use py script in mvc and don't know how to publish it on cpanel

  • Hi Elham. You seem to be new and are being downvoted. I imagine it is because you have not provided information on what you have tried, what error message you get, etc. Maybe some info on what you are trying to do/use the py script. – Devon Burriss Aug 28 '19 at 06:09
  • question is unclear. Why do u need python in .net , where do u need to copy it ? What the python.exe do? – Arunprasanth K V Aug 28 '19 at 13:26
  • @DevonBurriss yeah I'm new to stackoverflow. I have to edit my question again. Hope you will help me in this. – elham enayati Aug 28 '19 at 18:31
  • @ArunprasanthKV I edited it. Hope it will be clear now. – elham enayati Aug 28 '19 at 18:35
  • The effort people are willing to put into answering is often proportional to the effort you have put in to ask the question. So what have you tied? What errors are you getting? What output are you expecting from the py script. Why are you not using a Python web solution? – Devon Burriss Aug 28 '19 at 19:01
  • @DevonBurriss I'm not getting any errors. when i run the website locally, it works fine but now I wanna publish the website and the problem is that I don't know how to publish it when I have to call python file and .exe file. I get string from python file. I'm not using python web solution is that I just want to call a python file in mvc and get a string result. – elham enayati Aug 28 '19 at 19:21
  • Publish where? To a Web App Service in Azure is very different to a local IIS site on a local VM. This is the kind of information that would be needed for anyone to help you. Are you calling it via `Process` class? Why not show some more code of that? – Devon Burriss Aug 28 '19 at 19:24
  • @DevonBurriss I have edited my question and put my code there. – elham enayati Aug 28 '19 at 19:43
  • If it is working in Local more seems like your issue is because of the path. Are you using windows machine to publish your project ? Why cant u simply create a virtual directory inside solution folder and move file there and use relative path. ? – Arunprasanth K V Sep 04 '19 at 02:45

1 Answers1

0

I would suggest using ENVIRONMENT variables to set the path. This would allow you to have different paths for local development vs deployed. Hopefully, then CPANEL allows you to set these variables. Otherwise, you will need to deploy to a VM that you have more control of.

As an example your path string would now be:

string python = Environment.GetEnvironmentVariable("python_exe");
string myPythonApp = Environment.GetEnvironmentVariable("app_python_script");

In terms of actually deploying it, add it as an item and set it to Copy and the py script should be in the bin directory.

Devon Burriss
  • 2,497
  • 1
  • 19
  • 21
  • Environment variables gives the path of iisexpress path. Am I right ? How can I set the path ? Would you please make it more clear. – elham enayati Aug 30 '19 at 13:21