You want to use the automatic deployment scripts that Azure web apps provide, but your Django website needs Python 3.6? No problem, with your own deploy.cmd, this challenge will work.
Azure Web apps are an excellent way to automatically deploy your Django app on a push request. However, if you want to use a different Python version than the ones offered by Azure, this can lead to problems.
Installing Python 3.6
Azure web apps offer Python versions 2.7 and 3.4 as standard. However, it is possible to install other versions at a later date. To do this, go to the Kudu portal of your web app (https://yoursite.scm.azurewebsites.net) and click on "Site extensions". Under Gallery you can find all available extensions, including Python 3.6.4 x64.
After the installation you will find the additional Python version under "D:\home\", in my case the path looks like this: D:\home\python364x64\python.exe.
So far, so good. Unfortunately, that is not all. If you now try to deploy your web app, you will notice that the new Python version has not been adopted as the default Python version, it is still Python 2.7 or 3.4. The result is that the deployment process causes errors in the Python-specific part. Installing specific packages will not work.
Use your own deploy.cmd
To use the installed Python version within the deployment process, we have to customize the Kudo deploy.cmd.
First download the current deploy.cmd script from the Kudo portal. You will find it under Tools > Download deployment script.
In the root-folder of your app create a new file with the name: .deployment and put in the following code:
[config]
command = deploy.cmd
This file tells Kudu to use our own deploy.cmd script, which we customize for Python 3.6.4. Here we have to edit the :Deployment section.
First we tell the script to create a virtual environment with Python 3.6.4. In my case, I had to install virtualenv first.
:: 2. Create virtual environment
IF NOT EXIST "%DEPLOYMENT_TARGET%\env" (
echo Creating %PYTHON_RUNTIME% virtual environment.
D:\home\python364x64\python.exe -m pip install --upgrade pip
D:\home\python364x64\pip install virtualenv
D:\home\python364x64\python.exe -m virtualenv env
IF !ERRORLEVEL! NEQ 0 goto error
) ELSE (
echo Found compatible virtual environment.
)
Then install all packages from requirements.txt.
:: 3. Install packages
echo Pip install requirements.
env\Scripts\python.exe -m pip install --upgrade -r requirements.txt
IF !ERRORLEVEL! NEQ 0 goto error
With these changes a virtual environment with Python 3.6.4 will be created during the next deployment and the required packages based on the requirements.txt will be installed.
You can find the whole deploy.cmd file on GitHub:
https://github.com/phookycom/python364-deploy/blob/master/deploy.cmd
Cover image by Isaque Pereira | arrows-close-up-dark-energy-394377 | CC0 License