无法在Azure部署Python3.6 Flask应用:配置及依赖问题求助
Flask部署到Azure时遭遇
ModuleNotFoundError: No module named 'flask'及内部服务器错误 我已经花了好几天时间尝试把一个简单的Flask应用部署到Azure,查了无数谷歌资料,但始终没能成功。我的项目架构非常简单(下面会详细展示,我觉得问题应该不在这),也配置了web.config文件,但网站一直显示页面无法显示,发生内部服务器错误。
通过Kudu查看日志,发现核心问题是依赖包没安装,具体报错如下:
Traceback (most recent call last): File "D:\home\python364x64\wfastcgi.py", line 791, in main env, handler = read_wsgi_handler(response.physical_path) File "D:\home\python364x64\wfastcgi.py", line 633, in read_wsgi_handler handler = get_wsgi_handler(os.getenv("WSGI_HANDLER")) File "D:\home\python364x64\wfastcgi.py", line 616, in get_wsgi_handler raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb)) ValueError: "MentorQuestFlask.app" could not be imported: Traceback (most recent call last): File "D:\home\python364x64\wfastcgi.py", line 600, in get_wsgi_handler handler = __import__(module_name, fromlist=[name_list[0][0]]) File ".\MentorQuestFlask\__init__.py", line 5, in <module> from flask import Flask ModuleNotFoundError: No module named 'flask'
同时StdErr里也重复了相同的错误信息。
我已经做的配置操作:
- 在Azure中添加了Python 3.6.4 x64扩展
- 在配置选项卡中指定了Python 3.6版本
- 路径映射中配置:Extension为
fastCgi,Script processor为D:\home\python362x86\python.exe(这里和web.config里配置的364x64版本不一致),Argument为D:\home\python362x86\wfastcgi.py - 在应用设置中将
WEBSITE_USE_PLACEHOLDER设为0
我的项目架构:
项目根目录下有requirements.txt(包含所有依赖),文件结构如下:
runserver.py:
from os import environ from MentorQuestFlask import app if __name__ == '__main__': HOST = environ.get('SERVER_HOST', 'localhost') try: PORT = int(environ.get('SERVER_PORT', '5555')) except ValueError: PORT = 5555 app.run(HOST, PORT)
MentorQuestFlask/__init__.py:
from flask import Flask app = Flask(__name__) import MentorQuestFlask.views
MentorQuestFlask/views.py:
from MentorQuestFlask import app @app.route('/') @app.route('/home') def home(): return 'Hello World!'
web.config:
<?xml version="1.0"?> <configuration> <appSettings> <add key="WSGI_HANDLER" value="MentorQuestFlask.app"/> <add key="PYTHONPATH" value="D:\home\site\wwwroot"/> <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/> </appSettings> <system.webServer> <handlers> <add name="PythonHandler" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\python364x64\python.exe|D:\home\python364x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/> </handlers> <rewrite> <rules> <rule name="Static Files" stopProcessing="true"> <conditions> <add input="true" pattern="false" /> </conditions> </rule> <rule name="Configure Python" stopProcessing="true"> <match url="(.*)" ignoreCase="false" /> <conditions> <add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" /> </conditions> <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
我现在的疑问是:看起来Azure环境里根本没安装任何依赖包(不然不会找不到flask),而且web.config的配置可能也有问题——我试了好几种配置都没用,实在没辙了,恳请各位大佬给点帮助或者建议!
内容的提问来源于stack exchange,提问作者Charles Malec
相关产品推荐
相关产品推荐

