3
votes

I've tried for 3 days now and can't get this to work. I am following this tutorial from microsoft docs: https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-python-create-deploy-django-app

EDIT: I am using the windows command line option, NOT the Visual Studio. I am starting with the PTVS python/django template in azure marketplace as suggested by the tutorial.

The app works fine with sql lite, but when I change it to the sql server, it stops working with the following error:

django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'
Error was: No module named 'sql_server'
2017-04-27 04:34:34.525084: wfastcgi.py 2.1.1 closed

The app works for me locally by connecting to the same remote sql azure db, so the problem is definitely azure. I got the same error locally till I installed pyodbc-azure (https://github.com/michiya/django-pyodbc-azure). As suggested by Microsoft docs, and the library docs, the following is my Database connection specification in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'django-myorg',
        'USER': 'riz',
        'PASSWORD': '#######',
        'HOST': 'django-myorg.database.windows.net',
        'PORT': '',

        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
        },
    },
}

This is my third try setting this up, and I am frustrated by the lack of up-to-date documentation on Microsoft's part.

I have tried troubleshooting where I could by following this guide: https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-python-configure. I've tried building wheels for all the required libraries, but still having the same issue. I have a feeling it's one of the pyodbc libraries that Azure is not able to get by using pip.

Update: I removed .skipPythonDepolyment which was included by default in the PTVS template provided by azure for django. This prompted azure to actually try and install libraries listed in requirements.txt. This is the latest error according to pip.log:

running build

running build_ext

building 'pyodbc' extension

error: Unable to find vcvarsall.bat

this seems to be trying to build pyodbc but fails. Not sure what I can do at this point.

1
You got it to work locally but can't get it to work in production? Is this correct? Just sounds like django-pyodbc-azure isn't installed in the production environment.BottleZero
Are you running Django in a virtual env? From the commandline what does the "pip freeze" command print out? If you don't see the string "django-pyodbc-azure" you should install this app. BTW, as of now I only managed to get a specific version of that pkg (1.10.4.0) to work with a specific version of Django (1.10.4). i.e. latest Django (1.11) won't work.Paolo Stefan
@Bott0610 yep that's correct. Not sure why though it's not being installed and how to go about doing that. According to the guide, when I "git push azure master" it should automatically go through the requirements.txt and install all the dependencies listed.Riz
@PaoloStefan I'm publshing straight to azure web app if that's what you're asking. I do have a virtual env folder that gets pushed to azure as well. As for the "pip freeze" command, I have it installed locally (1.10.4.0) with Django (1.10.7). Pip is not available as a command on the Kudu console, so I'm not sure how to check it on Azure.Riz
@Riz There seems to be some mistakes about your db NAME, USER & HOSTin settings.py. Please check them with your SQL Azure connection string which looks like Driver={ODBC Driver 13 for SQL Server};Server=tcp:<db-server>.database.windows.net,1433;Database=<db-name>;Uid=<user>@<db-server>;Pwd={your_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30; Also you can refer to the other SO thread stackoverflow.com/questions/20666043/…Peter Pan

1 Answers

2
votes

I have a similar setup to you and have spent many months understanding how to get Django to work on Azure. There are a few things you should know up front because they could help solve your problem:

Python version

I had a number of issues with the web.config files, because it appears that Django apps do not load using the version of Python specified in "runtime.txt", like they say they do. They use 2.7 by default. To run Django using Python 3.4, you need to manually replace the web.config file with the version you want and make sure it's specifying the right version of Django. I believe the default Django app comes with three web.config files, one for 2.7, one for 3.4, and one titled web.config. Try copying the 3.4 one into web.config, which is the only file Azure actually uses.

Pip install from wheels

You will need to install a lot of libraries from wheels, most of which can be found at http://www.lfd.uci.edu/~gohlke/pythonlibs/. Be aware that you need to use the Python 3.4, 32-bit version on Azure (unless you've changed the default python and environment), and furthermore you need to replace the middle tag with none or it will break on Azure:

numpy‑1.13.1+mkl‑cp34‑cp34m‑win32.whl changes to numpy‑1.13.1+mkl‑cp34‑none‑win32.whl

To simplify installing from wheels, I added a wheelhouse folder to my app root folder on Azure, and moved my wheels in there. Then in requirements.txt I point to the exact wheel file in that wheelhouse that I want to install.

Django version

django-pyodbc-azure is required, and in my experience the latest build works with Django 1.11. However, if I remember correctly, the default Django app on the marketplace simply says django in its requirements.txt file, so if you have issues with connecting after you successfully install django-pyodbc-azure, try specifying a version of Django. I use the following and it works:

django>=1.11.1,<1.12
pyodbc from wheel
D:\home\site\wwwroot\wheelhouse\pyodbc-4.0.16-cp34-none-win32.whl
django-pyodbc-azure==1.11.0

Check your pip build output to make sure it's re-installing the right version of Django if you don't already have 1.11. I do recommend you specify in your requirements file that django should not be updated to 1.12 until you're ready, because otherwise it could be updated when the next release comes out and that would break the connection with django-pyodbc-azure.

Database connection

My database connection is a little different. If you're using an Azure SQL database, try one or more of the following:

  • Specify the port 1433
  • Add tcp: in front of your database host, e.g. tcp:django-myorg.database.windows.net - and make sure your SQL server is actually named django-myorg, this is separate from the name of your database
  • Try changing your driver to SQL Server Native Client 11.0

Most likely your settings are fine if they work on your computer, and the problem is in building the required modules - but just in case.

And have heart, this took me a few months to set up too - the process could certainly be made easier.