Use custom Python version in Windows App Service

Published Jul 07 2021 07:38 PM 2,101 Views
Microsoft

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
%3CLINGO-SUB%20id%3D%22lingo-sub-2521781%22%20slang%3D%22en-US%22%3EUse%20custom%20Python%20version%20in%20Windows%20App%20Service%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2521781%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20article%20is%20written%20to%20share%20how%20to%20deploy%20and%20configure%20your%20custom%20version%20of%20Python%20in%20Windows%20App%20Service.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBefore%20you%20continue%20reading%20the%20detailed%20steps%20of%20how%20to%20use%20custom%20version%20of%20Python%2C%20please%20notice%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EMicrosoft%20has%20deprecated%20the%20Python%20extensions%20for%20App%20Service%20on%20Windows%20as%20described%20in%20this%20article%20in%20favor%20of%20a%20direct%20deployment%20to%20App%20Service%20on%20Linux.%3C%2FLI%3E%0A%3CLI%3EIf%20your%20application%20can%20run%20on%20the%20following%20version%20of%20Python%2C%20you%20can%20use%20platform%20provided%20Python%20extensions%2C%20rather%20than%20deploy%20your%20own%20version%20of%20Python.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_0-1625639707075.png%22%20style%3D%22width%3A%20128px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293890i47C75E14BF505B0A%2Fimage-dimensions%2F128x229%3Fv%3Dv2%22%20width%3D%22128%22%20height%3D%22229%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_0-1625639707075.png%22%20alt%3D%22Hanli_Ren_0-1625639707075.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-2085454365%22%20id%3D%22toc-hId-2085453647%22%3EStep%201%3A%20Install%20preferred%20version%20of%20Python%20on%20your%20local%20machine%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3EDownload%20installation%20file%20from%20%3CA%20href%3D%22https%3A%2F%2Fwww.python.org%2Fdownloads%2Frelease%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fwww.python.org%2Fdownloads%2Frelease%3C%2FA%3E%3CBR%20%2F%3EFor%20example%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fwww.python.org%2Fdownloads%2Frelease%2Fpython-388%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fwww.python.org%2Fdownloads%2Frelease%2Fpython-388%2F%3C%2FA%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_1-1625639830914.png%22%20style%3D%22width%3A%20633px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293891i5FDDA12641E0F583%2Fimage-dimensions%2F633x212%3Fv%3Dv2%22%20width%3D%22633%22%20height%3D%22212%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_1-1625639830914.png%22%20alt%3D%22Hanli_Ren_1-1625639830914.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3ERun%20the%20python-3.8.8-amd64.exe%20in%20my%20local%20windows%20machine%EF%BC%9A%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_2-1625639944342.png%22%20style%3D%22width%3A%20410px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293892i0C17B1323A5DF5D2%2Fimage-dimensions%2F410x252%3Fv%3Dv2%22%20width%3D%22410%22%20height%3D%22252%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_2-1625639944342.png%22%20alt%3D%22Hanli_Ren_2-1625639944342.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3EMake%20sure%20check%20to%20install%20pip%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_3-1625639964188.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293893i69D26647A865A596%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_3-1625639964188.png%22%20alt%3D%22Hanli_Ren_3-1625639964188.png%22%20%2F%3E%3C%2FSPAN%3E%3CBR%20%2F%3ESelect%20the%20target%20path%20to%20install%20python%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_4-1625639986464.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293894i3497AC0A49EF318C%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_4-1625639986464.png%22%20alt%3D%22Hanli_Ren_4-1625639986464.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3ECompress%20the%20whole%20Python388%20folder%20to%20Python38.zip%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_5-1625640018093.png%22%20style%3D%22width%3A%20487px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293895iDD4B139F9904084F%2Fimage-dimensions%2F487x73%3Fv%3Dv2%22%20width%3D%22487%22%20height%3D%2273%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_5-1625640018093.png%22%20alt%3D%22Hanli_Ren_5-1625640018093.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3EInside%20the%20zip%20file%2C%20it%20should%20contains%20all%20the%20following%20contents%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_6-1625640066339.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293896i96B0B0562C74F77C%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_6-1625640066339.png%22%20alt%3D%22Hanli_Ren_6-1625640066339.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH3%20id%3D%22toc-hId-277999902%22%20id%3D%22toc-hId-277999184%22%3EStep%202%3A%20Upload%20your%20Python%20to%20Windows%20App%20Service%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3EGo%20to%20your%20web%20app%20Kudu%20site%20https%3A%2F%2F%3CWEB-APP-NAME%3E.scm.azurewebsites.net%2FDebugConsole%3CBR%20%2F%3EUnder%20D%3A%5Chome%5C%2C%20drag%20your%20Python38.zip%20file%20to%20the%20Kudu%20console.%3CBR%20%2F%3EIt%20will%20automatically%20unzip%20it.%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_7-1625640126955.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293897i10ACA9C0CAA96F52%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_7-1625640126955.png%22%20alt%3D%22Hanli_Ren_7-1625640126955.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3EYou%20can%20see%20the%20new%20%E2%80%9CPython38%E2%80%9D%20folder%20being%20generated.%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_8-1625640149060.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293898iCE4B0185C43E5CB3%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_8-1625640149060.png%22%20alt%3D%22Hanli_Ren_8-1625640149060.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FWEB-APP-NAME%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH3%20id%3D%22toc-hId--1529454561%22%20id%3D%22toc-hId--1529455279%22%3EStep%203%3A%20Add%20the%20custom%20Python%20binary%20into%20the%20PATH%20environment%20variable%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3ECreate%20an%20applicationhost.xdt%20file%20in%20D%3A%5Chome%5Csite%20folder.%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_9-1625640209248.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293899i74CD5ED383B92E0A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_9-1625640209248.png%22%20alt%3D%22Hanli_Ren_9-1625640209248.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3ESample%20applicationhost.xdt%20file%20contents%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%20%20%0A%20%3CCONFIGURATION%20xdt%3D%22http%3A%2F%2Fschemas.microsoft.com%2FXML-Document-Transform%22%3E%20%0A%20%20%20%3CSYSTEM.WEBSERVER%3E%20%0A%20%20%20%20%20%3CRUNTIME%20transform%3D%22InsertIfMissing%22%3E%0A%20%20%20%20%20%20%20%3CENVIRONMENTVARIABLES%20transform%3D%22InsertIfMissing%22%3E%0A%20%20%20%20%20%20%20%20%20%3CADD%20name%3D%22PATH%22%20value%3D%22%25HOME%25%5CPython388%5C%3B%25PATH%25%22%20locator%3D%22Match(name)%22%20transform%3D%22InsertIfMissing%22%3E%3C%2FADD%3E%20%20%20%20%0A%20%20%20%20%20%20%20%3C%2FENVIRONMENTVARIABLES%3E%0A%20%20%20%20%20%3C%2FRUNTIME%3E%20%0A%20%20%20%3C%2FSYSTEM.WEBSERVER%3E%20%0A%3C%2FCONFIGURATION%3E%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLI%3E%0A%3CLI%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EThen%20Restart%20the%20Web%20App.%3C%2FSPAN%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EEvery%20time%20you%20Restart%20the%20web%20app%2C%20the%20platform%20will%20check%20your%20D%3A%5Chome%5Csites%5Capplicationhost.xdt%20file%2C%20use%20it%20to%20transfer%20the%20D%3A%5Clocal%5CConfig%5Capplicationhost.config.%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EThe%20transform%20logs%20are%20all%20recorded%20in%20D%3A%5Chome%5CLogFiles%5CTransform%2C%20you%20can%20check%20the%20timestamp%20and%20file%20name%20to%20get%20the%20latest%20log.%3C%2FSPAN%3E%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_10-1625640338087.png%22%20style%3D%22width%3A%20565px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293900i42FD1FA04F641B81%2Fimage-dimensions%2F565x214%3Fv%3Dv2%22%20width%3D%22565%22%20height%3D%22214%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_10-1625640338087.png%22%20alt%3D%22Hanli_Ren_10-1625640338087.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3ECheck%20your%20D%3A%5Clocal%5CConfig%5Capplicationhost.config%20file%2C%20should%20see%20Python%20path%20being%20added%20as%20the%20%E2%80%9CPATH%E2%80%9D%20environment%20variables.%3C%2FSPAN%3E%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_11-1625640368045.png%22%20style%3D%22width%3A%20602px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293901i2C801B27307012BB%2Fimage-dimensions%2F602x354%3Fv%3Dv2%22%20width%3D%22602%22%20height%3D%22354%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_11-1625640368045.png%22%20alt%3D%22Hanli_Ren_11-1625640368045.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3EIn%20the%20Kudu%20Powershell%20console%2C%20use%20%E2%80%9Cpython%20-V%E2%80%9D%20to%20check%20Python%20version.%20You%20should%20be%20able%20to%20see%20the%20customer%20Python%20version.%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Hanli_Ren_12-1625640428400.png%22%20style%3D%22width%3A%20537px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F293902i2EAE05B9193E57EA%2Fimage-dimensions%2F537x444%3Fv%3Dv2%22%20width%3D%22537%22%20height%3D%22444%22%20role%3D%22button%22%20title%3D%22Hanli_Ren_12-1625640428400.png%22%20alt%3D%22Hanli_Ren_12-1625640428400.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2521781%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPython%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Jul 07 2021 07:38 PM
Updated by: