這個快速入門指南教你如何打造一個簡單的 Python 應用程式,能夠:
- 連接Azure Cache for Redis
- 將當前的日期和時間寫入 Redis
- 讀回數值
- 將結果列印到控制台
你會使用 GitHub Copilot 來產生大部分程式碼和配置步驟。
先決條件
如需完整的安裝指示,請參閱 開始使用 一文。 請確保您有下列項目:
一個 Azure 帳號和 Azure 訂閱的存取權。 如需如何設定它們的詳細資訊,請參閱 Azure 帳戶的定價頁面。
GitHub 帳戶和 GitHub Copilot 訂用帳戶。 關於如何設定,請參考在GitHub建立帳號和快速入門GitHub Copilot。
Visual Studio Code。 如需如何下載並安裝的詳細資訊,請參閱 設定Visual Studio Code。
GitHub Copilot 延伸模組和 GitHub Copilot 聊天延伸模組。 關於如何安裝這些擴充功能的說明,請分別參考 在 VS Code 中設定 GitHub Copilot 及 在 VS Code 中開始使用 GitHub Copilot Chat。
Important
GitHub Copilot 是由 GitHub 管理的個別訂用帳戶。 有關GitHub Copilot訂閱與支援的問題,請參見 Getting Start with a GitHub Copilot Plan。
一個 Azure 帳號和 Azure 訂閱的存取權。 如需如何設定它們的詳細資訊,請參閱 Azure 帳戶的定價頁面。
GitHub 帳戶和 GitHub Copilot 訂用帳戶。 關於如何設定,請參考在GitHub建立帳號和快速入門GitHub Copilot。
Visual Studio 2022 (任何版本)。 關於如何下載與安裝,請參見 Install Visual Studio。
Important
GitHub Copilot 是由 GitHub 管理的個別訂用帳戶。 有關GitHub Copilot訂閱與支援的問題,請參見 Getting Start with a GitHub Copilot Plan。
一個 Azure 帳號和 Azure 訂閱的存取權。 如需如何設定它們的詳細資訊,請參閱 Azure 帳戶的定價頁面。
GitHub 帳戶和 GitHub Copilot 訂用帳戶。 關於如何設定,請參考在GitHub建立帳號和快速入門GitHub Copilot。
Visual Studio 2026 (任何版本)。 關於如何下載與安裝,請參見 Install Visual Studio。
Important
GitHub Copilot 是由 GitHub 管理的個別訂用帳戶。 有關GitHub Copilot訂閱與支援的問題,請參見 Getting Start with a GitHub Copilot Plan。
開發應用程式
請依照本文所述步驟操作:
- 在您的工作區中建立一個
.env檔案,以環境變數的形式儲存 Azure 部署資訊。 - 寫一個提示,在你的訂閱中建立 Azure Cache for Redis 的實例。 Redis 連線資訊也儲存在檔案中
.env。 - 確認資源與
.env檔案的建立是否正確。 - 撰寫一段指令來建立一個 Python 應用程式,使用環境變數來擷取、寫入和讀取快取。
- 驗證應用程式是否有效。
- 清理 Azure 裡的資源。
確保你選擇了正確的工具
你必須同時安裝 Azure MCP Server 和 GitHub Copilot for Azure。
- 在聊天視窗中選擇 「配置工具... 」圖示。
- 配置工具 會顯示在指令面板中。 請確保「Azure MCP」和「GitHub Copilot for Azure」的頂端節點都被選中。
- 在聊天視窗中選擇 「選擇工具... 」圖示。
- 會顯示「選擇工具」選單。 請確定「Azure MCP Server」頂端節點已被選擇。
- 在聊天視窗中選擇 「選擇工具 」圖示。
- 會顯示「 選擇工具 」選單。 確保「Azure」和「Azure MCP」的頂端節點都被選中。
建立本地環境變數
一個常見的開發做法是將重要的鍵和其他設定以環境變數 .env 的形式儲存在工作區資料夾中的檔案中。 這樣可以讓所有設定都獨立於專案內。
Important
確保你的 .gitignore 檔案包含 .env ,避免不小心將秘密提交給原始碼控制。
在此步驟中,請使用類似以下提示在工作區建立 .env 檔案:
Create a .env file in this workspace with the following environment variables filled in:
AZURE_SUBSCRIPTION_ID
AZURE_TENANT_ID
AZURE_LOCATION
AZURE_RESOURCE_GROUP
AZURE_RESOURCE_PREFIX
Use my <your-subscription-name> subscription and I want to put everything in eastus.
請將<your-subscription-name>替換成你Azure訂閱的名稱。 Copilot會幫你查詢訂閱和租戶 ID,產生資源群組名稱和前綴,並建立 .env 檔案。
檔案建立完成後,打開它並確認數值是否正確:
AZURE_SUBSCRIPTION_ID=<your-azure-subscription-id>
AZURE_TENANT_ID=<your-azure-tenant-id>
AZURE_LOCATION=eastus
AZURE_RESOURCE_GROUP=<resource-group>
AZURE_RESOURCE_PREFIX=<resource-prefix>
建立 Azure Cache for Redis
打開 GitHub Copilot Chat,並貼上以下提示:
You have access to Azure MCP tools.
Use the variables in the `.env` file in this workspace to create an Azure Cache for Redis instance.
Tasks:
1. Ensure the resource group exists.
2. Create Azure Cache for Redis:
- Name: {AZURE_RESOURCE_PREFIX}-redis
- SKU: Basic C0
- TLS enabled (port 6380)
3. Write the following values into the `.env` file:
REDIS_HOST
REDIS_PORT=6380
REDIS_PASSWORD (primary key)
REDIS_SSL=true
Important:
- Use Azure MCP to create resources and fetch keys.
Copilot建立 Redis 資源,然後建立一個包含主機名稱、主鍵及其他環境變數的 .env 檔案。
確認 .env 檔案中有 Redis 的設定
打開專案資料夾裡的檔案
.env,確認它有沒有值。REDIS_HOST=<your-cache-name>.redis.cache.windows.net REDIS_PORT=6380 REDIS_PASSWORD=<primary-key> REDIS_SSL=true請使用以下提示符驗證 Azure Cache for Redis 實例是否正在執行。
Use the values in the `.env` file in this workspace to validate that an instance of Azure Cache for Redis is running and ready to be used.
撰寫 Python 應用程式的提示
請使用以下提示詞建立一個 Python 應用程式,從新的 Azure Cache for Redis 實例中寫入和讀取。
Create a minimal Python console app in this workspace.
Important:
- Do ALL work directly by editing files.
- Do NOT ask me to copy/paste code.
- Create files if they do not exist.
Goal:
Build a simple app that writes the current date/time to Azure Cache for Redis, reads it back, and prints results to the console.
Project requirements:
1. Create or update these files:
- main.py
- requirements.txt
- .gitignore
2. requirements.txt must include:
- redis
- python-dotenv
3. .gitignore must include:
- .venv/
- __pycache__/
- .env
4. main.py must:
- Load environment variables using python-dotenv
- Read:
REDIS_HOST
REDIS_PORT
REDIS_PASSWORD
REDIS_SSL
- Connect to Azure Cache for Redis using TLS (ssl=True when REDIS_SSL=true)
- Use decode_responses=True
- Test connection with PING and print:
Connected to Redis
- Write current datetime (ISO format) to key:
demo:timestamp
- Read the value back
- Print exactly:
WROTE: <value>
READ : <value>
- Wrap connection logic in a try/except and print a helpful error message.
5. Keep the code simple and beginner-friendly:
- Single file
- No classes
- About 40–60 lines
After editing the files:
- Show a summary of what you changed.
- Do NOT print the full file contents unless I ask.
驗證 Python 應用程式
確保你在提示中要求的檔案確實存在。 用肉眼檢查檔案,看看它們的數值是否合理。
檢查
main.py檔案,確保它從.env檔案取得數值,匯入redis套件,並連接到 Azure Cache for Redis。 檢查它是否能寫入快取並讀取。 你可能會看到類似以下程式碼的程式碼:import os from datetime import datetime from dotenv import load_dotenv import redis # Load local environment variables load_dotenv() host = os.getenv("REDIS_HOST") port = int(os.getenv("REDIS_PORT", "6380")) password = os.getenv("REDIS_PASSWORD") ssl_enabled = os.getenv("REDIS_SSL", "true").lower() == "true" try: client = redis.Redis( host=host, port=port, password=password, ssl=ssl_enabled, decode_responses=True ) # Verify connection client.ping() print("Connected to Redis") # Write current time now = datetime.now().isoformat() client.set("demo:timestamp", now) print(f"WROTE: {now}") # Read value back value = client.get("demo:timestamp") print(f"READ : {value}") except Exception as ex: print("Connection failed.") print(ex)Important
AI 輔助軟體開發是非確定性的,意即不會產生相同的程式碼兩次。 然而,在像這樣的簡單應用中,基本方法、語法和最終結果應該相近,雖然不完全相同。
執行應用程式
在終端機裡執行這個應用程式:
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
python main.py
您應該會看到類似以下的輸出:
Connected to Redis
WROTE: 2026-03-01T10:22:11.452331
READ : 2026-03-01T10:22:11.452331
清理資源
使用下列提示:
I am finished with this instance. Please remove the Azure Cache for Redis that you created earlier by using the values in the `.env` file. ONLY remove this resource and nothing else.
相關內容
- 了解Azure的GitHub Copilot是什麼,以及它是怎麼運作的。
- 請遵循quickstart,了解如何在軟體開發流程中加入GitHub Copilot for Azure。 快速入門說明如何將服務部署到 Azure、監控其狀態並排除問題。
- 請參閱學習更多關於Azure及了解您的Azure帳號、訂閱與資源範例提示。
- 請參閱設計 及開發 Azure 應用程式的範例提示。
- 請參閱部署應用程式到 Azure 的範例提示。
- 請參閱排除 Azure 資源問題的範例提示。