Wednesday, July 7, 2021

Use custom Python version in Windows App Service

The article is written to share how to deploy and configure your custom version of Python in Windows App Service.

 

Before you continue reading the detailed steps of how to use custom version of Python, please notice:

  1. Microsoft has deprecated the Python extensions for App Service on Windows as described in this article in favor of a direct deployment to App Service on Linux.
  2. If your application can run on the following version of Python, you can use platform provided Python extensions, rather than deploy your own version of Python.

Hanli_Ren_0-1625639707075.png

 

Step 1: Install preferred version of Python on your local machine

Step 2: Upload your Python to Windows App Service

  • Go to your web app Kudu site https://<web-app-name>.scm.azurewebsites.net/DebugConsole
    Under D:\home\, drag your Python38.zip file to the Kudu console.
    It will automatically unzip it.
    Hanli_Ren_7-1625640126955.png

    You can see the new “Python38” folder being generated.

    Hanli_Ren_8-1625640149060.png

     

Step 3: Add the custom Python binary into the PATH environment variable

  • Create an applicationhost.xdt file in D:\home\site folder.
    Hanli_Ren_9-1625640209248.png

    Sample applicationhost.xdt file contents:

    	<?xml version="1.0"?> 
    	<configuration xmlns:xdt= http://schemas.microsoft.com/XML-Document-Transform> 
    	  <system.webServer> 
    	    <runtime xdt:Transform="InsertIfMissing">
    	      <environmentVariables xdt:Transform="InsertIfMissing">
    	        <add name="PATH" value="%HOME%\Python388\;%PATH%" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" />    
    	      </environmentVariables>
    	    </runtime> 
    	  </system.webServer> 
    </configuration>​
  • Then Restart the Web App.Every time you Restart the web app, the platform will check your D:\home\sites\applicationhost.xdt file, use it to transfer the D:\local\Config\applicationhost.config.

 

  • The transform logs are all recorded in D:\home\LogFiles\Transform, you can check the timestamp and file name to get the latest log.

    Hanli_Ren_10-1625640338087.png

    Check your D:\local\Config\applicationhost.config file, should see Python path being added as the “PATH” environment variables.

    Hanli_Ren_11-1625640368045.png

     

  • In the Kudu Powershell console, use “python -V” to check Python version. You should be able to see the customer Python version.

    Hanli_Ren_12-1625640428400.png
Posted at https://sl.advdat.com/3hokjGr