Search code examples
pythonazureflaskfastcgiazure-web-app-service

ValueError: "hostingstart.app" could not be imported


Trying to create azure python web app for flask python, but getting the following error

Error occurred while reading WSGI handler:

Traceback (most recent call last):   File "D:\Python34\Scripts\wfastcgi.py", line 711, in main
    env, handler = read_wsgi_handler(response.physical_path)   File "D:\Python34\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
    return env, get_wsgi_handler(handler_name)   File "D:\Python34\Scripts\wfastcgi.py", line 551, in get_wsgi_handler
    raise ValueError('"%s" could not be imported' % handler_name) ValueError: "hostingstart.app" could not be imported

StdOut:

StdErr:

I tried to upgrade wfastcgi, after that i have changed to script location to new wfastcgi, it was throwing scripts handlers scriptProcessor could not be found in error

Folder structure:

WWWroot
 - hostingstart.py
 - view.py
 - web.config

hostingstart.py

from flask import Flask
app = Flask(__name__)

import view
wsgi_app = app.wsgi_app

Web.Config

<configuration>   <appSettings>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
    <add key="WSGI_HANDLER" value="hostingstart.app"/>
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>   </appSettings>   <system.webServer>
     <httpErrors errorMode="Detailed"></httpErrors>
     <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule"           
    scriptProcessor="D:\Python34\python.exe|D:\Python34\Scripts\wfastcgi.py"
          resourceType="Unspecified" requireAccess="Script" />
    </handlers>   </system.webServer> </configuration>

Solution

  • You could try to install python extension version following below steps,instead of using azure web app self-brought versions.

    Step 1 : Add Extensions(here is Python 3.6.1 x64)

    enter image description here

    Step 2 : Publish your flask project and add the web.config.

    web.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="WSGI_HANDLER" value="<Your Project Name>.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="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\Python361x64\python.exe|D:\home\Python361x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
        </handlers>
      </system.webServer>
    </configuration>
    

    If you deploy successfully, you could see following structure in the KUDU path: D:\home\site\wwwroot>.

    enter image description here

    If you want to use additional python package, please go on.

    Step 3: Switch to the Kudu CMD and commands cd Python361x64 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

    Step 4 : Install any packages you need via python -m pip install pyodbc

    enter image description here