2

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?

Ilya Pukhov
  • 153
  • 1
  • 11

2 Answers2

1

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.

ThePretendProgrammer
  • 1,449
  • 10
  • 16
1

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.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • Whow, that is super cool, thank you! If that works, than it means that the issue with my app is not in the abscence of the driver but somwhere else. I wish I had a way to see my application logs somwhere in azure, do you know if there is one? I have toggled the logs in the App service settings and it produces a log.txt file tha can be accessed in Kudu. But there are only server logs and no logs that I am used to see in Python. – Ilya Pukhov Nov 10 '17 at 20:28
  • @IlyaPukhov Hi,sorry for the late reply. I suggest you use application diagnostics to check the application log. Please refer to this page:https://learn.microsoft.com/en-us/azure/app-service/web-sites-enable-diagnostic-log. – Jay Gong Nov 13 '17 at 01:59
  • @IlyaPukhov Hi, any progress now? – Jay Gong Nov 15 '17 at 01:21
  • I am stuck with this. Here is my connection string and all: `server = os.environ['SERVER'] database = os.environ['DATABASE'] username = os.environ['DBUSERNAME'] password = os.environ['PASSWORD'] driver= '{ODBC Driver 13 for SQL Server}' cnxn = pyodbc.connect('DRIVER='+driver+';PORT=1433;SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+ password) cursor = cnxn.cursor() cursor.execute("select top(1) * from faces") row = cursor.fetchall()` it works at the local machine but when pushed to azure it says: – Ilya Pukhov Nov 17 '17 at 19:31
  • " Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application." – Ilya Pukhov Nov 17 '17 at 19:36
  • There is no python errors in the log.txt file, it just says: "2017-11-17 19:29:07.371000: wfastcgi.py 2.1.1 started 2017-11-17 19:29:07.387000: Python version: 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] 2017-11-17 19:29:07.403000: wfastcgi.py 2.1.1 initializing 2017-11-17 19:29:07.418000: Activating virtualenv with D:\home\site\wwwroot\env\Scripts\activate_this.py – Ilya Pukhov Nov 17 '17 at 19:37
  • 2017-11-17 19:29:07.434000: Getting handler main.app 2017-11-17 19:29:12.594000: Got handler: 2017-11-17 19:29:12.594000: wfastcgi.py will restart when files in D:\home\site\wwwroot\ are changed: .*((\.py)|(\.config))$ 2017-11-17 19:29:12.610000: wfastcgi.py 2.1.1 initialized" – Ilya Pukhov Nov 17 '17 at 19:37
  • I kind of dont know how to investigate the problem further – Ilya Pukhov Nov 17 '17 at 19:37
  • My suspiction was for the firewall settings of the SQL server, but I checked the application IP and added it ti the firewall rules – Ilya Pukhov Nov 17 '17 at 20:17
  • @IlyaPukhov Sorry for the late reply. Your python code works fine locally ,but error occurred on the azure. I think it's the configuration issue.I update my web.config in my answer.Would you please check it or post your configuration to let me help you solve the issue. – Jay Gong Nov 21 '17 at 06:20
  • My web config is ` ` – Ilya Pukhov Nov 22 '17 at 09:24
  • @IlyaPukhov Please check my Update Answer 2 . I share my working steps with you. Please follow my steps and try again. – Jay Gong Nov 23 '17 at 02:32
  • @IlyaPukhov Hi, any progress now? – Jay Gong Nov 27 '17 at 01:14
  • I use python 2.7, can this be an issue? – Ilya Pukhov Nov 27 '17 at 14:22
  • @IlyaPukhov I don't think it's a python version issue, have you tried the web. config file I provided? – Jay Gong Nov 28 '17 at 01:18
  • I am confused about your web___.cinfig file since it has the `` line there, and I am using python2.7, so I have to modify it somehow. I browsed the folders with KUDU and did not find any Python folders at my Web App machine. Do you have any idea on how to create the proper config file? – Ilya Pukhov Nov 29 '17 at 15:02
  • I tried to use the virtualenv_proxy.py and web.2.7.config files from the template in Azure documentation but it doesnot work. Meaning that the app starts, but when you try to navigate to different page it says "the resource not found" – Ilya Pukhov Nov 29 '17 at 15:12
  • it has a single linh that is supposed to take you to the https://datasetlabelapp.azurewebsites.net/label page by: – Ilya Pukhov Nov 29 '17 at 15:12
  • `@app.route('/label') def label(): return render_template('label.html')` – Ilya Pukhov Nov 29 '17 at 15:13