5

I would like to execute Django migrate command on azure app service in my application,

$ python manage.py migrate

but I have no idea how to do this.

Cody
  • 4,353
  • 4
  • 39
  • 42

3 Answers3

5

As common scenario, we leverage virtual environment to handler python scripts as the official guide shows. If so, it may raise exceptions if we use the Azure Python runtime to run the commands because of lacking of dependencies.

Usually, we can leverage Kudu Console site of your Web Apps or Visual Studio Online extension to modifying scripts or executing commands.

Kudu Console site:

  1. You can login the Kudu Console site whose url is https://<your_web_app_name>.scm.azurewebsites.net/DebugConsole
  2. cd to d:\home\site\wwwroot which is the root directory of your application.
  3. run the command env\Scripts\python.exe manage.py migrate (assume your virtual environment is env in the root directory)

Visual Studio Online extension:

  1. Install VSO extensio, you can refer the answer of How to install composer on app service?
  2. Login VSO editor site, find the open console button to open the cmdlet for commands, you can find this button in the left navigation bar.enter image description here

Any further concern, please feel free to let me know.

Community
  • 1
  • 1
Gary Liu
  • 13,758
  • 1
  • 17
  • 32
  • Kudo service is a cool tool. Thanks for your sharing. I haven't know it before, and I got this video for a quick intro: https://azure.microsoft.com/en-us/documentation/videos/super-secret-kudu-debug-console-for-azure-web-sites/ – Cody Apr 11 '16 at 06:12
  • Hi i'm facing the same problem and i cannot find where is my python env in kudu, any idea how i can find it – snowflake Dec 08 '19 at 10:00
3

You can run Python code inside your Azure web application. You have to make sure Python is enabled for the app though:

enter image description here

Then - you can probably wrap your call to python manage.py migrate in a batch script and call it in the startup task for your webapp.

Startup tasks are described here: https://azure.microsoft.com/en-us/documentation/articles/cloud-services-startup-tasks/ and what it boils down to is that you have to bundle your batch script with your application, and modify the ServiceDefinition.csdef and add the startup task in the XML like so:

<Startup>
    <Task commandLine="Startup.cmd" executionContext="limited" taskType="simple" >
        <Environment>
            <Variable name="MyVersionNumber" value="1.0.0.0" />
        </Environment>
    </Task>
</Startup>
Jochen van Wylick
  • 5,303
  • 4
  • 42
  • 64
0

In addition to running a command from the Kudu Console as Gary Liu suggested, I found creating a WebJob superior for long running commands (which seem to timeout/ not work very well at all on Azure)

Create a WebJob and use type Triggered and triggers Manual

enter image description here

Upload a *.cmd file with your command inside like

d:\home\python364x64\python.exe d:\home\site\wwwroot\manage.py migrate

Replace the first path with whatever/wherever your python is located

Then Click "Run" whenever you want to run your Django Command

enter image description here

uosjead
  • 426
  • 6
  • 5