2

I want to create a WebJob in C#. Unfortunately, I need to use a Python 3 script as there is currently no suitable library for 1 particular task that I need to perform, using C#.

For example, see this answer/example.

Is it possible to have my WebJob call a Python script? I can place the Python3 script in a blob container - would I then be able to call and execute it from my C# WebJob?

pookie
  • 3,796
  • 6
  • 49
  • 105

2 Answers2

4

Sure, it's possible to call a Python 3 script via C# WebJob that follow the sample you linked. First of all, you need to install a Python 3 runtime as below.

  1. Access the kudu tool via the url https://<your webapp name>.scm.azurewebsites.net, and follow the figure below to install a Python 3 runtime. enter image description here
  2. For example to install Python 3.5.2 x86, it will be installed in the path D:\home\Python35, then you just need to change the Python execute file path in the sample to try to run it.

Hope it helps.


Update: Install pip tool & other Python packages.

  1. Access the url https://<your azure webapp name>.scm.azurewebsites.net/DebugConsole.
  2. Commands as below.

    D:\home>cd Python35
    D:\home\Python35>curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    
    100 1558k  100 1558k    0     0  6829k      0 --:--:-- --:--:-- --:--:-- 7179k
    D:\home\Python35>python get-pip.py
    Requirement already up-to-date: pip in d:\home\python35\lib\site-packages
    Collecting wheel
      Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    Installing collected packages: wheel
    Successfully installed wheel-0.29.0
    
  3. For example, install numpy package

    D:\home\Python35>python get-pip.py numpy
    Collecting numpy
      Downloading numpy-1.13.1-cp35-none-win32.whl (6.8MB)
    Installing collected packages: numpy
    Successfully installed numpy-1.13.1
    
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Thanks! What if I have other python libraries being used by my script (the script uses NetCDF4, for example)? – pookie Jul 12 '17 at 09:26
  • @pookie First, you need to download [`get-pip.py`](https://pip.pypa.io/en/stable/installing/) script file to install `pip` tool in the python runtime path. Then, you can use `Scripts/pip install ` to install the packages you need. – Peter Pan Jul 12 '17 at 09:46
  • Sorry, but I don't fully understand. I am familiar with pip and installing packages, but I am not sure how to execute my `Scripts/pip install ` command. I'm also not sure what you mean by `install pip tool in the python runtime path` - will that `get-pip.py` do that for me? For example, I can't install `get-pip.py` via the kudu tool. What do I do with this file? – pookie Jul 12 '17 at 09:54
  • @pookie I updated my reply for how to install pip & use it to install more packages. – Peter Pan Jul 13 '17 at 07:45
  • @PeterPan Do you mind taking a look at [here](https://stackoverflow.com/questions/57830172/path-to-execute-python-script-from-azure-webjob)? I assume the path is not set correctly. – bbusdriver Sep 07 '19 at 02:13
  • @bbusdriver I will try to help you to fix it. – Peter Pan Sep 07 '19 at 08:53
1

Don't know if it is the best approach but this is what I did in the past:

  • Create a python webjob (manual, triggered) (see tutorial) Create Azure Webjob - Manual Triggered

  • Create a C# webjob.

  • Trigger the Python job from the C# job:

    using (var client = new HttpClient())
    {
        var username = "jobusername";
        var password = "jobpassword";
        var byteArray = Encoding.ASCII.GetBytes($"{username}:{password}");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
        var response = await client.PostAsync("joburl", null);
    }
    

You can find the job credentials from the job properties on azure portal: Azure webjob properties

Thomas
  • 24,234
  • 6
  • 81
  • 125