Search code examples
pythondjangoazuredjango-pyodbc-azure

Getting server error upon publishing django application to azure


I am trying to publish my django app to azure with a MSSQL server. I am using django-azure-pyodbc with pyodbc to connect and while it works locally, whenever I publish with mssql I get an internal server error although the publish succeeds. If I publish with the sqllite3 server that comes prepackaged, it works. I am using a virtual environment with Python 3.4, Django 1.8.4, pyodbc 3.0.10 and django-pyodbc-azure 1.8.3.0. My settings.py file is as follows.

DATABASES = {
   'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'db_name',
        'USER': 'u_name',
        'PASSWORD': 'p_word',
        'HOST': 'host.database.windows.net',
        'PORT': '',
    }
}

I have added the webapp's outgoing IP addresses to the server hoping that was the issue but it was not. After checking the diagnostic logs I found a more detailed explanation than the 500 error but I'm not sure I understand it.

 ModuleName FastCgiModule

 Data1 FASTCGI_RESPONSE_ERROR

Data2 Error occurred while reading WSGI handler: Traceback (most recent call last): File "D:\home\site\wwwroot\env\lib\site-packages\sql_server\pyodbc\base.py", line 14, in import pyodbc as Database ImportError: DLL load failed: %1 is not a valid Win32 application. During handling of the above exception, another exception occurred: 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 541, in get_wsgi_handler handler = handler() File ".\ptvs_virtualenv_proxy.py", line 120, in get_venv_handler handler = get_wsgi_handler(os.getenv('WSGI_ALT_VIRTUALENV_HANDLER')) File ".\ptvs_virtualenv_proxy.py", line 78, in get_wsgi_handler handler = handler() File "D:\home\site\wwwroot\env\lib\site-packages\django\core\wsgi.py", line 14, in get_wsgi_application django.setup() File "D:\home\site\wwwroot\env\lib\site-packages\django__init__.py", line 18, in setup apps.populate(settings.INSTALLED_APPS) File "D:\home\site\wwwroot\env\lib\site-packages\django\apps\registry.py", line 108, in populate app_config.import_models(all_models) File "D:\home\site\wwwroot\env\lib\site-packages\django\apps\config.py", line 198, in import_models self.models_module = import_module(models_module_name) File "D:\Python34\lib\importlib__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 2254, in _gcd_import File "", line 2237, in _find_and_load File "", line 2226, in _find_and_load_unlocked File "", line 1200, in _load_unlocked File "", line 1129, in _exec File "", line 1471, in exec_module File "", line 321, in _call_with_frames_removed File "D:\home\site\wwwroot\env\lib\site-packages\django\contrib\auth\models.py", line 41, in class Permission(models.Model): File "D:\home\site\wwwroot\env\lib\site-packages\django\db\models\base.py", line 139, in new new_class.add_to_class('_meta', Options(meta, **kwargs)) File "D:\home\site\wwwroot\env\lib\site-packages\django\db\models\base.py", line 324, in add_to_class value.contribute_to_class(cls, name) File "D:\home\site\wwwroot\env\lib\site-packages\django\db\models\options.py", line 250, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "D:\home\site\wwwroot\env\lib\site-packages\django\db__init__.py", line 36, in getattr return getattr(connections[DEFAULT_DB_ALIAS], item) File "D:\home\site\wwwroot\env\lib\site-packages\django\db\utils.py", line 240, in getitem backend = load_backend(db['ENGINE']) File "D:\home\site\wwwroot\env\lib\site-packages\django\db\utils.py", line 111, in load_backend return import_module('%s.base' % backend_name) File "D:\Python34\lib\importlib__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 2254, in _gcd_import File "", line 2237, in _find_and_load File "", line 2226, in _find_and_load_unlocked File "", line 1200, in _load_unlocked File "", line 1129, in _exec File "", line 1471, in exec_module File "", line 321, in _call_with_frames_removed File "D:\home\site\wwwroot\env\lib\site-packages\sql_server\pyodbc\base.py", line 16, in raise ImproperlyConfigured("Error loading pyodbc module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: DLL load failed: %1 is not a valid Win32 application. StdOut: StdErr:


Solution

  • I do not really know how Azure works, but I got this error when mixing 32 and 64 bit driver/python combo's. Try using a x64 bit version of python and the driver.