Reputation: 6686
I've tried for 3 days now and can't get this to work. I am following this tutorial from microsoft docs: https://learn.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://learn.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.
Upvotes: 3
Views: 1417
Reputation: 111
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:
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.
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-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
.
My database connection is a little different. If you're using an Azure SQL database, try one or more of the following:
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 databaseSQL 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.
Upvotes: 2