Publishing MVC app that uses python script

I currently have MVC project that calls python script via Process (new processStartinfo(“/path/to/python.exe”, ” /path/to/script.py”). Which works perfectly fine in visual studio.
When i publish this on azure how will it be able to call python.exe? (Im probably not constructing this question accurately since this is my very first web to publish and do not have full understanding of publishing)

P.S. I did try to use IronPython but since my script uses NLTK, i ran into bunch of issues so it was easier to install python&nltk with pip, then call python.exe with through command line that takes argument of /path/to/script.py. Any input is appreciated.

EDIT: my homeController starts a process that passes in path_of_python, path_of_script to command line, redirects the output and does work on the data it gets back (from output).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It seems to be possible to use Python script with NLTK package in C# on Azure website.

I tried to implement it via a workaround way as below, and it works fine.

Step 1. For installing Python & NLTK on Azure WebApp

  1. Access the Kudu tool of your webapp via the url https://<your webapp name>.scm.azurewebsites.net.
  2. Install a site extension Python 2.7.12 x86 which will be installed at the path D:home if using the 32bit version of Azure WebApp as example.
    enter image description here
  3. Switch to the Kudu CMD, then you can see a new Python runtime which has been installed at here and you have permission to do any operations on it.
    enter image description here
  4. Commands cd Python27 and touch get-pip.py and copy the content of the url https://bootstrap.pypa.io/get-pip.py into the get-pip.py via Edit button, then run python get-pip.py to install the pip tool.
    enter image description here
  5. Command Scriptspip install nltk to install nltk package.
  6. To download the nltk data, command python -m nltk.downloader -d D:homePython27nltk_data all as below, please don’t close the current browser window or switch other urls until the command completed
    Publishing MVC app that uses python script
    You can view the download task in process as below via the url https://<your webapp name>.scm.azurewebsites.net/ProcessExplorer/ at the other browser window.
    Publishing MVC app that uses python script
    6.1 Or you can download the nltk data on local to upload them into Azure WebApp.

Step 2. For testing Python script with NLTK package

  1. Command touch test.py at the path wwwroot, and edit the content below.
    import nltk
    sentence = """At eight o'clock on Thursday morning
    ... Arthur didn't feel very good."""
    tokens = nltk.word_tokenize(sentence)
    print tokens
  2. The console show the result as below, it works.
    enter image description here

Step 3. Calls python script via Process in C#

Just only using the absoulte path of python runtime & script D:homePython27python & D:homesitewwwroottest.python instead of them in your C# code.

Please try and feedback your result. Any concern, please feel free to let me know.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x