发布使用python脚本的MVC应用 [英] Publishing MVC app that uses python script

查看:57
本文介绍了发布使用python脚本的MVC应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个MVC项目,该项目可通过Process调用python脚本(新的processStartinfo("/path/to/python.exe","/path/to/script.py").在Visual Studio中运行良好. 当我在azure上发布此内容时,它将如何调用python.exe? (由于我是第一个要发布的网站,并且对发布没有完全的了解,因此我可能无法准确地构造出这个问题)

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.我确实尝试使用IronPython,但是由于我的脚本使用了NLTK,所以遇到了很多问题,因此使用pip安装python& nltk更加容易,然后通过带有/path/to/script参数的命令行调用python.exe .py.任何输入表示赞赏.

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.

我的homeController启动了一个进程,该进程将path_of_python,path_of_script传递给命令行,重定向输出并对其返回的数据(从输出)进行处理.

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

推荐答案

在Azure网站上的C#中,似乎可以将Python脚本与NLTK包一起使用.

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.

步骤1.用于安装Python& Azure WebApp上的NLTK

  1. 通过URL https://<your webapp name>.scm.azurewebsites.net访问Web应用的Kudu工具.
  2. 安装站点扩展Python 2.7.12 x86,如果使用32位版本的Azure WebApp作为示例,该扩展将安装在路径D:\home上.
  3. 切换到Kudu CMD,然后您会看到一个新的Python运行时,该运行时已安装在此处,并且您有权对其进行任何操作.
  4. 命令cd Python27touch get-pip.py,并通过Edit按钮将URL https://bootstrap.pypa.io/get-pip.py的内容复制到get-pip.py,然后运行python get-pip.py安装pip工具.
  5. 命令Scripts\pip install nltk安装nltk软件包.
  6. 要下载nltk数据,请按以下命令python -m nltk.downloader -d D:\home\Python27\nltk_data all进行操作,直到命令完成,请不要关闭当前浏览器窗口或切换其他URL. 您可以在另一个浏览器窗口中通过URL https://<your webapp name>.scm.azurewebsites.net/ProcessExplorer/查看正在执行的下载任务,如下所示. 6.1或者,您可以在本地下载nltk数据以将其上传到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.
  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.
  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.
  5. Command Scripts\pip install nltk to install nltk package.
  6. To download the nltk data, command python -m nltk.downloader -d D:\home\Python27\nltk_data all as below, please don't close the current browser window or switch other urls until the command completed 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. 6.1 Or you can download the nltk data on local to upload them into Azure WebApp.

步骤2.用于使用NLTK软件包测试Python脚本

    在路径wwwroot
  1. 命令touch test.py,然后编辑下面的内容.

  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

  • 控制台显示结果如下,它可以正常工作.

  • The console show the result as below, it works.

    第3步.在C#中通过Process调用python脚本

    Step 3. Calls python script via Process in C#

    仅使用python运行时&的绝对路径脚本D:\home\Python27\python& D:\home\site\wwwroot\test.python而不是您的C#代码中的它们.

    Just only using the absoulte path of python runtime & script D:\home\Python27\python & D:\home\site\wwwroot\test.python instead of them in your C# code.

    请尝试并反馈您的结果.如有任何疑问,请随时告诉我.

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

    这篇关于发布使用python脚本的MVC应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆