Jason
Jason

Reputation: 43

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:

Upvotes: 1

Views: 1068

Answers (3)

Jason
Jason

Reputation: 43

The issue was my coworker who is publishing was using the 64 bit version of python in his environment. Simple enough but I appreciate the help.

Upvotes: 1

Gary Liu
Gary Liu

Reputation: 13918

I cannot reproduce your issue, but I did a test with your packages and versions. And I used Visual Studio to publish the Django application to Azure Web Apps, it worked fine. Here are my steps for your reference: 1,Create an empty Web App and set up deployment from local Git repo

2,Create a Django application in Visual Studio, add Virtual Environment with python 3.4, then install python packages, here is the content in requirements.txt: Django==1.8.4 pyodbc==3.0.10 django-pyodbc-azure==1.8.3.0

3,Modify the database settings to MSSQL on Azure, press F5 to test on local

4,Right click Django application project name, click publish to deploy

enter image description here

After finishing the deployment, it will automatically browse your application in default browser.

Upvotes: 0

Ward
Ward

Reputation: 2852

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.

Upvotes: 1

Related Questions