Search code examples
djangopython-3.xazureazure-web-app-serviceazure-deployment

How to change Azure Git CI deployment default python version


Azure only supports Python version 2.7 and 3.4 in the Application settings, and I installed newer Python 3.6.2 via App Service for my django application. I followed the setup for continuous integration with Azure & GitHub and found out that deployment failed when Azure is running deployment command. Below is part of the log that shows Azure decided to use default 2.7 even tho I specified to use 3.6.2 in web.config file

Detected requirements.txt.  You can skip Python specific steps with a .skipPythonDeployment file.
Detecting Python runtime from site configuration
Detected python-2.7
Creating python-2.7 virtual environment.
...
#(and it just continue and install the requirements.txt with pip using python-2.7 which failed)

Azure will determine the version of Python to use for its virtual environment with the following priority:

  1. version specified in runtime.txt in the root folder
  2. version specified by Python setting in the web app configuration (the Settings > Application Settings blade for your web app in the Azure Portal)
  3. python-2.7 is the default if none of the above are specified

I can't specify the version using runtime.txt since 3.6.2 is not a valid value for the content. It looks like Azure ignored my web.config and just jump to use 2.7 as the default since none of the above are specified.

As of now, I had to go in Kudu console and manually deploy my app using 3.6.2. How can I set it to use 3.6.2 as the default when deploying my code from Github?

Below is my web.config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
    <!-- Django apps only: change the project name to match your app -->
    <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()"/>
    <add key="DJANGO_SETTINGS_MODULE" value="mysite.settings" />
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
  </appSettings>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule"
           scriptProcessor="D:\home\python362x86\python.exe|D:\home\python362x86\wfastcgi.py"
           resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
  </system.webServer>
</configuration>

Solution

  • Per my experience , if you're using python Extensions in the Azure Python web app , you do not have to choose Python versions in the application settings.

    enter image description here

    Please refer to the steps I did as below:

    Step 1 : Create azure web app and add Extensions(here is Python 3.6.2 x86)

    enter image description here

    Step 2 : Prepare your django project and add the web.config.

    web.config:

    <configuration>
      <appSettings>
        <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()"/>
        <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
        <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
        <add key="DJANGO_SETTINGS_MODULE" value="<your project name>.settings" />
      </appSettings>
      <system.webServer>
        <handlers>
          <add name="PythonHandler" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\python362x86\python.exe|D:\home\python362x86\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>
    

    Step 3 : Add your site URL domain to ALLOWED_HOSTS in settings.py in your django project

    ALLOWED_HOSTS = ['***.azurewebsites.net']
    

    Step 4 : Publish your django project via FTP or Git.

    Step 5: Switch to the Kudu CMD and commands cd Python362x86 and touch get-pip.py and copy the content of the url https://bootstrap.pypa.io/get-pip.py into the get-pip.py via Edit button, then run python get-pip.py to install the pip tool.

    enter image description here

    Step 6 : Install django package or any packages you need via python -m pip install ***

    enter image description here

    Then access your domain url successfully.

    You could also refer to the official tutorial.

    Hope it helps you.


    Update Answer:

    According to the screenshots and logs you provide, I see that the Python is 2.7 version is detected by the application in runtime .

    I check the environment variable on KUDU and find the Python 2.7 version.

    enter image description here

    I recommend that you overwrite the Python version in environment variable with the version of the extensions Python version you used.

    enter image description here

    Please try again.