2
votes

I am running a simple Python web app in the Azure app service that needs to acces an Azure SQL database. In order to que an Azure SQL database from python one needs to install the ODBC driver. I have done that at my local machine and it works perfectly. How do I install the ODBC driver at the Azure machine that is running my app or if it is nescessary at all?

2
Hi,what's your local machine system? Windows?Jay Gong
Yes, it is WindowsIlya Pukhov

2 Answers

1
votes

Per my experience , the Azure Web App Service Runtime is Windows system. That does not require a reload driver.

I tried to access Azure SQL Database in my flask web app.

You could refer to my working code.

view.py

from datetime import datetime
from flask import render_template
from jaygongflask import app
import pyodbc

@app.route('/database')
def database():
    """Renders the about page."""
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=***.database.windows.net;DATABASE=***;UID=***;PWD=***')
    cursor = cnxn.cursor()
    cursor.execute("select * from dbo.Student")
    row = cursor.fetchall()
    #for r in row:
     #   print r
    return render_template(
        'database.html',
        title='Database',
        year=datetime.now().year,
        message='Database query result.',
        queryResult = row
    )

Install pyodbc package

Here, I use python361x64 extension. So I run the command python -m pip install pyodbc in KUDU.

enter image description here

Get query result

Access the url http://***.azurewebsites.net/database .

enter image description here

Hope it helps you.Any concern, please let me know.


Update Answer :

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="WSGI_HANDLER" value="jaygongflask.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>

Update Answer 2:

My web app works with python361x64 extension. Please refer to the steps I did as below:

Step 1 : Create azure web app and 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>

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 pyodbc package or any packages you need via python -m pip install pyodbc

enter image description here

More deployment details , please refer to this tutorial.

1
votes

Unfortunately, you can't install custom drivers on Azure App Service. You would need an IaaS offering for that. This question has already been asked here.