Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

c# - 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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:homePython27 ltk_data all as below, please don't close the current browser window or switch other urls until the command completed enter image description here 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. enter image description here 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:homesitewwwrootest.python instead of them in your C# code.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...