在Codespace 上運行Python
範例原始碼放在 GitHub Repo: vscode-remote-try-python
參考文章:Setting up a Python project for GitHub Codespaces
1. 在 codespace 上開啟 project
Go to https://github.com/microsoft/vscode-remote-try-python.
Click Use this template, then
點擊 Open in a codespace.
2: 新增 dev container 設定檔案
1.使用 Visual Studio Code 指令 (Ctrl+Shift+P/Shift+Command+P),
然後輸入 add dev
點擊 Codespaces: Add Dev Container Configuration Files.
點擊 Create a new configuration.
因為已經存在’.devcontainer/devcontainer.json’ 檔案所以未出現這個提示,這邊選擇 Continue
選擇 Show All Definitions.
輸入 python 然後選擇 Python 3
選擇 python 版本 這邊我們選擇預設值
輸入 py 選擇 Coverage.py (via pipx) 然後點 OK
3. 修改 devcontainer.json 檔案
修改 devcontainer.json 檔案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:0-3.11-bullseye",
"features": {
"ghcr.io/devcontainers-contrib/features/coverage-py:2": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pip3 install --user -r requirements.txt",
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"streetsidesoftware.code-spell-checker"
]
}
}
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}(Ctrl+Shift+P) 輸入 rebuild
選擇 Codespaces: Rebuild Container.
4. 接下來就能運行 python 了
測試輸入指令:
1 | python -m flask run |
選擇 Open in Browser
會看到以下畫面