Create and run Azure Functions locally by using the Core Tools (Python)

Note: สร้าง functions project ด้วย vscode extension ง่ายกว่าสร้างด้วยคำสั่ง func init (Create an Azure Function by using Visual Studio Code) เพราะจะสร้างไฟล์ config ต่างๆของ vscode และ .venv มาให้ด้วย , สร้างด้วยคำสั่งมีประโยชน์ตอนสร้าง function ด้วย func new

Create a new functions project with func init

สร้าง functions project ชื่อ FunctionPy2207

> mkdir FunctionPy2207
> cd .\FunctionPy2207\

To create a new functions project, run func init on the command line.

> func init
Use the up/down arrow keys to select a worker runtime:
dotnet (isolated process)

> func init
Use the up/down arrow keys to select a worker runtime:python
Found Python version 3.9.10 (py).
Writing requirements.txt
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing C:\Project\FunctionPy2207\.vscode\extensions.json

ดูไฟล์ที่คำสั่ง func init สร้างมาให้ เมื่อเลือกเป็น python

$ tree
├── host.json
├── local.settings.json
└── requirements.txt

0 directories, 4 files


  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"


  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true"


# Do not include azure-functions-worker as it may conflict with the Azure Functions platform


## Getting Started with Azure Function
### Last updated: March 8th 2021

#### Project Structure
The main project folder (<project_root>) can contain the following files:

* **local.settings.json** - Used to store app settings and connection strings when running locally. This file doesn't get published to Azure. To learn more, see [local.settings.file](
* **requirements.txt** - Contains the list of Python packages the system installs when publishing to Azure.
* **host.json** - Contains global configuration options that affect all functions in a function app. This file does get published to Azure. Not all options are supported when running locally. To learn more, see [host.json](
* **.vscode/** - (Optional) Contains store VSCode configuration. To learn more, see [VSCode setting](
* **.venv/** - (Optional) Contains a Python virtual environment used by local development.
* **Dockerfile** - (Optional) Used when publishing your project in a [custom container](
* **tests/** - (Optional) Contains the test cases of your function app. For more information, see [Unit Testing](
* **.funcignore** - (Optional) Declares files that shouldn't get published to Azure. Usually, this file contains .vscode/ to ignore your editor setting, .venv/ to ignore local Python virtual environment, tests/ to ignore test cases, and local.settings.json to prevent local app settings being published.

Each function has its own code file and binding configuration file ([**function.json**](

#### Developing your first Python function using VS Code

If you have not already, please checkout our [quickstart]( to get you started with Azure Functions developments in Python. 

#### Publishing your function app to Azure 

For more information on deployment options for Azure Functions, please visit this [guide](

#### Next Steps

* To learn more about developing Azure Functions, please visit [Azure Functions Developer Guide](

* To learn more specific guidance on developing Azure Functions with Python, please visit [Azure Functions Developer Python Guide](

Create a new function with func new

สร้าง function ชื่อ HttpExample โดยใช้ template แบบ HttpTrigger

> func new
Use the up/down arrow keys to select a template:
Azure Blob Storage trigger
Azure Cosmos DB trigger
Durable Functions activity
Durable Functions entity
Durable Functions HTTP starter
Durable Functions orchestrator
Azure Event Grid trigger
Azure Event Hub trigger
HTTP trigger
Kafka output
Kafka trigger
Azure Queue Storage trigger
RabbitMQ trigger
Azure Service Bus Queue trigger
Azure Service Bus Topic trigger
Timer trigger

> func new
Use the up/down arrow keys to select a template:HTTP trigger
Function name: [HttpTrigger] HttpExample
Writing C:\Project\FunctionPy2207\HttpExample\
Writing C:\Project\FunctionPy2207\HttpExample\function.json
The function "HttpExample" was created successfully from the "HTTP trigger" template.

จะได้ไฟล์ HttpExample/ และไฟล์ HttpExample/function.json


import logging

import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:'Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
            req_body = req.get_json()
        except ValueError:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",


  "scriptFile": "",
  "bindings": [
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
      "type": "http",
      "direction": "out",
      "name": "$return"

Run functions locally

> func start
Found Python version 3.9.10 (py).

Azure Functions Core Tools
Core Tools Version:       4.0.4629 Commit hash: N/A  (64-bit)
Function Runtime Version:


        HttpExample: [GET,POST] http://localhost:7071/api/HttpExample

For detailed output, run func with --verbose flag.

ติดตั้ง package เพิ่ม

สร้าง Virtual Environments ชื่อ .venv

> python -m venv .venv


> .\.venv\Scripts\activate

ติดตั้ง numpy

> python -m pip install numpy

ไฟล์ requirements.txt

# Do not include azure-functions-worker as it may conflict with the Azure Functions platform


เพิ่ม import ที่ไฟล์

import numpy

ลองรัน func start ถ้าไม่ error ก็ติดตั้ง package สำเร็จ