2

I've built a simple Django application on my local Windows machine that connects to a SQL Server hosted on Azure by leveraging the Django-Pyodbc-Azure back end. I'm able to connect to the database fine on my local machine and my app runs without issue.

However, I'm not in the process of deploying the application to Azure's app service and I'm running into problems. The deployment itself runs without issue, however the following error message shows up in my logs:

Traceback (most recent call last): File "/home/site/wwwroot/antenv3.6/lib/python3.6/site-packages/sql_server/pyodbc/base.py", line 15, in <module>
import pyodbc as Database
ImportError: libodbc.so.2: cannot open shared object file: No such file or directory

File "/home/site/wwwroot/antenv3.6/lib/python3.6/site-packages/sql_server/pyodbc/base.py", line 17, in <module>
raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: libodbc.so.2: cannot open shared object file: No such file or directory

My requirements.txt file looks as such:

Django==2.1.4
django-pyodbc-azure==2.1.0.0
pyodbc==4.0.25
pytz==2018.7

And again... this runs fine locally on my Windows machine. But I get this error when I deploy to Azure.

I suspect this has something to do with the Pyodbc backend not being installed correctly on Azure's LINUX based app service? Does anybody have experience resolving this?

2 Answers 2

1

I was running into this same issue with the pyodbc package when using the Linux version. I was using the Windows based web-app but had to rebuild from scratch and accidentally selected the Linux version.

Once switching back to the Windows version I used python 3.6 and followed these steps, (based on this blog for deploying a flask app, https://blogs.msdn.microsoft.com/pythonengineering/2016/08/04/upgrading-python-on-azure-app-service/):

Deploy a Django web-app to Azure

  1. In the portal click on "Web App">"New"
  2. Give the web app a name: <webappname>
  3. Set the resource group to be the same as the database
  4. Select the OS to be Windows
  5. Click, deploy - This will take few minutes to deploy
  6. Once deployed you can click on the URL https://<webappname>.azurewebsites.net to see a default azure web page
  7. In your Django project go to settings.py and in the "ALLOWED_HOSTS" add ".azurewebsites.net".
  8. In the Azure portal>webapp, go to Extensions and install python3.6 x64
  9. In Portal > webapp > Application settings, select "Always on".
  10. In settings.py set:
    • DEBUG = os.getenv('DJANGO_DEBUG') != 'FALSE'
    • also set other private variables as environment variables (these should then be added as key-value pairs (e.g. DJANGO_DEBUG = FALSE) in Portal>webapp>Application settings>Application settings.
  11. Also, to connect to the SQL database set the database settings to be:
    • OPTIONS[driver] = 'SQL Server Native Client 11.0'
    • OPTIONS[MARS_Connection] = 'True'
  12. Make sure you have the following files in the base directory of your django project:
    • ptvs_virtual_proxy.py
    • .SkipPythonDeployment
    • web.config make sure you have key="DJANGO_SETTINGS_MODULE" value "<django-project-name>.settings" (see below)
  13. In your local project, Run pip freeze and put the contents into requirements.txt (see below)
  14. Check that all migration files have been added to git by running git status
  15. Commit and push the changes to your git repo
  16. In the portal, in your new web-app go to "Deployment options"
  17. "Choose Source" -> Follow the steps and click "Finish/Ok" to see the deployment progress and view the logs
  18. Once deployment says "Success", Go to Kudu (http://<webappname>.scm.azurewebsites.net)>Powershell, and run D:\home\python364x64\python.exe -m pip install --upgrade -r D:\home\site\wwwroot\requirements.txt, This should hopefully be successful and only needs to be rerun when you update a package.
  19. Then go back to "Deployment Center" and click on the logs button, and click redeploy, you may also need to restart the web-app to update the environment variables (go to webapp>overview>restart).

  20. For static files, either commit all the admin files after running locally python manage.py collectstatic or you can setup static files like this, then run D:\home\python364x64\python.exe D:\home\site\wwwroot\manage.py collectstatic in Kudu)

web.config file:

<configuration>
<appSettings>
    <add key="WSGI_HANDLER" value="<django-project-name>.wsgi.application"/>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
    <add key="DJANGO_SETTINGS_MODULE" value="<django-project-name>.settings" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <httpErrors errorMode="Detailed"></httpErrors>
    <handlers>
      <add name="PythonHandler" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\python364x64\python.exe|D:\home\python364x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="Static Files" stopProcessing="true">
          <conditions>
            <add input="true" pattern="false" />
          </conditions>
        </rule>
        <rule name="Configure Python" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

requirements.txt file:

cycler==0.10.0
Django==1.11.15
django-pyodbc-azure==1.11.15.0
djangorestframework==3.6.3
djangorestframework-jsonp==1.0.2
pyodbc==4.0.25
Sign up to request clarification or add additional context in comments.

1 Comment

Good point @Pretasoc, I have modified my answer to include a step-by-step.
0

please go to the scm site of your web app, then try to manually install the modules which throw the error.

For detailed steps, please refer to this article.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.