

AWS App Runner 自 2026 年 4 月 30 日起，不再開放給新客戶。如果您想要使用 App Runner，請在該日期之前註冊。現有客戶可以繼續正常使用該服務。如需詳細資訊，請參閱[AWS App Runner 可用性變更](https://docs.aws.amazon.com/apprunner/latest/dg/apprunner-availability-change.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Python 平台
<a name="service-source-code-python"></a>

**重要**  
App Runner 將於 2025 年 12 月 1 日結束對 **Python 3.7** 和 **Python 3.8** 的支援。如需建議和詳細資訊，請參閱 [受管執行時間版本的終止支援](service-source-code.md#service-source-code.managed-platforms.eos)。

 AWS App Runner Python 平台提供受管執行期。每個執行時間都可讓您根據 Python 版本使用 Web 應用程式輕鬆建置和執行容器。當您使用 Python 執行期時，App Runner 會從受管 Python 執行期映像開始。此映像以 [Amazon Linux Docker 映像](https://hub.docker.com/_/amazonlinux)為基礎，包含 Python 版本的執行時間套件，以及一些工具和熱門的相依性套件。App Runner 使用此受管執行期映像做為基礎映像，並新增您的應用程式程式碼來建置 Docker 映像。然後，它會部署此映像，以在容器中執行您的 Web 服務。

 當您使用 App Runner 主控台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html) API 操作[建立服務時，](manage-create.md)您可以指定 App Runner 服務的執行時間。您也可以指定執行時間做為原始程式碼的一部分。在程式碼儲存庫中包含的 [App Runner 組態檔案中](config-file.md)使用 `runtime`關鍵字。受管執行時間的命名慣例為 *<language-name><major-version>*。

如需有效的 Python 執行期名稱和版本，請參閱 [Python 執行期發行資訊](service-source-code-python-releases.md)。

App Runner 會在每次部署或服務更新時，將服務的執行時間更新為最新版本。如果您的應用程式需要特定版本的受管執行時間，您可以使用 [App Runner 組態檔案中](config-file.md)的 `runtime-version`關鍵字來指定它。您可以鎖定任何層級的版本，包括主要或次要版本。App Runner 只會對服務的執行時間進行較低層級的更新。

Python 執行時間的版本語法： `major[.minor[.patch]]`

例如：`3.8.5`

下列範例示範版本鎖定：
+ `3.8` – 鎖定主要和次要版本。App Runner 只會更新修補程式版本。
+ `3.8.5` – 鎖定至特定修補程式版本。App Runner 不會更新您的執行時間版本。

**Topics**
+ [Python 執行期組態](#service-source-code-python.config)
+ [特定執行時間版本的呼叫](#service-source-code-python.callouts)
+ [Python 執行時間範例](#service-source-code-python.examples)
+ [Python 執行期發行資訊](service-source-code-python-releases.md)

## Python 執行期組態
<a name="service-source-code-python.config"></a>

當您選擇受管執行時間時，您也必須至少設定建置和執行命令。您可以在[建立](manage-create.md)或[更新](manage-configure.md) App Runner 服務時設定它們。您可以使用下列其中一種方法執行此操作：
+ **使用 App Runner 主控台** – 在建立程序或組態索引標籤的設定**建置**區段中指定命令。
+ **使用 App Runner API** – 呼叫 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html) 或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html) API 操作。使用 [CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html) 資料類型的 `BuildCommand`和 `StartCommand`成員指定命令。
+ **使用[組態檔案](config-file.md)** – 在最多三個建置階段指定一或多個建置命令，以及用於啟動應用程式的單一執行命令。還有其他選用的組態設定。

提供組態檔案是選用的。當您使用主控台或 API 建立 App Runner 服務時，您可以指定 App Runner 在建立時直接從組態檔案取得您的組態設定。

## 特定執行時間版本的呼叫
<a name="service-source-code-python.callouts"></a>

**注意**  
App Runner 現在會根據下列執行時間版本執行應用程式的更新建置程序：Python 3.11、Node.js 22 和 Node.js 18。如果您的應用程式在其中一個執行時間版本上執行，請參閱 [受管執行期版本和 App Runner 組建](service-source-code.md#service-source-code.build-detail) 以取得修訂建置程序的詳細資訊。使用所有其他執行時間版本的應用程式不會受到影響，而且會繼續使用原始建置程序。

### Python 3.11 （已修訂 App Runner 組建）
<a name="service-source-code-python.callouts.python311"></a>

針對受管 Python 3.11 執行時間，在 *apprunner.yaml* 中使用下列設定。
+ 將頂端區段中的`runtime`金鑰設定為 `python311`   
**Example**  

  ```
  runtime: python311
  ```
+ 使用 `pip3`而非 `pip` 安裝相依性。
+ 使用`python3`解譯器而非 `python`。
+ 以`pre-run`命令執行`pip3`安裝程式。Python 會在 `/app`目錄之外安裝相依性。由於 App Runner 會針對 Python 3.11 執行修訂後的 App Runner 組建，因此透過 `apprunner.yaml` 檔案組建區段中的命令在`/app`目錄外安裝的任何內容都會遺失。如需詳細資訊，請參閱[修訂後的 App Runner 組建](service-source-code.md#service-source-code.build-detail.v2)。  
**Example**  

  ```
  run:
    runtime-version: 3.11
    pre-run:  
      - pip3 install pipenv
      - pipenv install
      - python3 copy-global-files.py
    command: pipenv run gunicorn django_apprunner.wsgi --log-file -
  ```

如需詳細資訊，請參閱本主題稍後[的 Python 3.11 擴充組態檔案範例。](#service-source-code-python.examples.extended-v2)

## Python 執行時間範例
<a name="service-source-code-python.examples"></a>

下列範例顯示用於建置和執行 Python 服務的 App Runner 組態檔案。最後一個範例是完整 Python 應用程式的原始程式碼，您可以部署到 Python 執行時間服務。

**注意**  
這些範例中使用的執行時間版本為 *3.7.7* 和 *3.11。*您可以將其取代為您想要使用的版本。如需最新支援的 Python 執行期版本，請參閱 [Python 執行期發行資訊](service-source-code-python-releases.md)。

### 最小 Python 組態檔案
<a name="service-source-code-python.examples.minimal"></a>

此範例顯示您可以搭配 Python 受管執行時間使用的最小組態檔案。如需 App Runner 使用最小組態檔案所做的假設，請參閱 [組態檔案範例](config-file-examples.md#config-file-examples.managed)。

Python 3.11 使用 `pip3`和 `python3`命令。如需詳細資訊，請參閱本主題稍後[的 Python 3.11 擴充組態檔案範例。](#service-source-code-python.examples.extended-v2)

**Example apprunner.yaml**  

```
version: 1.0
runtime: python3 
build:
  commands:
    build:
      - pip install pipenv
      - pipenv install 
run: 
  command: python app.py
```

### 擴充 Python 組態檔案
<a name="service-source-code-python.examples.extended"></a>

此範例顯示使用具有 Python 受管執行時間的所有組態金鑰。

**注意**  
這些範例中使用的執行時間版本是 *3.7.7*。您可以將其取代為您想要使用的版本。如需最新支援的 Python 執行期版本，請參閱 [Python 執行期發行資訊](service-source-code-python-releases.md)。  
Python 3.11 使用 `pip3`和 `python3`命令。如需詳細資訊，請參閱本主題稍後的 Python 3.11 擴充組態檔案範例。

**Example apprunner.yaml**  

```
version: 1.0
runtime: python3 
build:
  commands:
    pre-build:
      - wget -c https://s3.amazonaws.com/amzn-s3-demo-bucket/test-lib.tar.gz -O - | tar -xz
    build:        
      - pip install pipenv
      - pipenv install
    post-build:
      - python manage.py test
  env:
    - name: DJANGO_SETTINGS_MODULE
      value: "django_apprunner.settings"
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 3.7.7
  command: pipenv run gunicorn django_apprunner.wsgi --log-file -
  network: 
    port: 8000
    env: MY_APP_PORT  
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
  secrets:
    - name: my-secret
      value-from: "arn:aws:secretsmanager:us-east-1:123456789012:secret:testingstackAppRunnerConstr-kJFXde2ULKbT-S7t8xR:username::"
    - name: my-parameter
      value-from: "arn:aws:ssm:us-east-1:123456789012:parameter/parameter-name"
    - name: my-parameter-only-name
      value-from: "parameter-name"
```

### 擴充 Python 組態檔案 — Python 3.11 （使用修訂的組建）
<a name="service-source-code-python.examples.extended-v2"></a>

此範例顯示在 中使用所有組態金鑰搭配 Python 3.11 受管執行時間。 `apprunner.yaml`此範例包含 `pre-run`區段，因為此版本的 Python 使用修訂過的 App Runner 建置。

只有修訂後的 App Runner 組建才支援 `pre-run` 參數。如果您的應用程式使用原始 App Runner 組建支援的執行時間版本，請勿將此參數插入您的組態檔案中。如需詳細資訊，請參閱[受管執行期版本和 App Runner 組建](service-source-code.md#service-source-code.build-detail)。

**注意**  
這些範例中使用的執行時間版本為 *3.11。*您可以將其取代為您想要使用的版本。如需最新支援的 Python 執行期版本，請參閱 [Python 執行期發行資訊](service-source-code-python-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: python311
build:
  commands:
    pre-build:
      - wget -c https://s3.amazonaws.com/amzn-s3-demo-bucket/test-lib.tar.gz -O - | tar -xz
    build:        
      - pip3 install pipenv
      - pipenv install
    post-build:
      - python3 manage.py test
  env:
    - name: DJANGO_SETTINGS_MODULE
      value: "django_apprunner.settings"
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 3.11
  pre-run:  
    - pip3 install pipenv
    - pipenv install
    - python3 copy-global-files.py
  command: pipenv run gunicorn django_apprunner.wsgi --log-file -
  network: 
    port: 8000
    env: MY_APP_PORT  
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
  secrets:
    - name: my-secret
      value-from: "arn:aws:secretsmanager:us-east-1:123456789012:secret:testingstackAppRunnerConstr-kJFXde2ULKbT-S7t8xR:username::"
    - name: my-parameter
      value-from: "arn:aws:ssm:us-east-1:123456789012:parameter/parameter-name"
    - name: my-parameter-only-name
      value-from: "parameter-name"
```

### 完成 Python 應用程式來源
<a name="service-source-code-python.examples.end2end"></a>

此範例顯示您可以部署到 Python 執行期服務的完整 Python 應用程式的原始程式碼。

**Example requirements.txt**  

```
pyramid==2.0
```

**Example server.py**  

```
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
import os

def hello_world(request):
    name = os.environ.get('NAME')
    if name == None or len(name) == 0:
        name = "world"
    message = "Hello, " + name + "!\n"
    return Response(message)

if __name__ == '__main__':
    port = int(os.environ.get("PORT"))
    with Configurator() as config:
        config.add_route('hello', '/')
        config.add_view(hello_world, route_name='hello')
        app = config.make_wsgi_app()
    server = make_server('0.0.0.0', port, app)
    server.serve_forever()
```

**Example apprunner.yaml**  

```
version: 1.0
runtime: python3
build:
  commands:
    build:
      - pip install -r requirements.txt
run:
  command: python server.py
```

# Python 執行期發行資訊
<a name="service-source-code-python-releases"></a>

**重要**  
App Runner 將於 2025 年 12 月 1 日結束對 **Python 3.7** 和 **Python 3.8** 的支援。如需建議和詳細資訊，請參閱 [受管執行時間版本的終止支援](service-source-code.md#service-source-code.managed-platforms.eos)。

本主題列出 App Runner 支援的 Python 執行期版本的完整詳細資訊。


**支援的執行時間版本 — 修訂後的 App Runner 組建**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/apprunner/latest/dg/service-source-code-python-releases.html)

**備註**  
**Python 3.11** – 對於使用 Python 3.11 受管執行時間的服務建置組態，我們提供特定建議。如需詳細資訊，請參閱 *Python 平台*主題[特定執行時間版本的呼叫](service-source-code-python.md#service-source-code-python.callouts)中的 。
App Runner 為最近發佈的特定主要執行時間提供修訂後的建置程序。因此，您會在本文件的特定章節中看到已*修訂的 App Runner 組建*和*原始 App Runner 組建*的參考。如需詳細資訊，請參閱 [受管執行期版本和 App Runner 組建](service-source-code.md#service-source-code.build-detail)。


**支援的執行時間版本 — 原始 App Runner 組建**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/apprunner/latest/dg/service-source-code-python-releases.html)

**注意**  
App Runner 為最近發佈的特定主要執行時間提供修訂後的建置程序。因此，您會在本文件的特定章節中看到已*修訂的 App Runner 組建*和*原始 App Runner 組建*的參考。如需詳細資訊，請參閱 [受管執行期版本和 App Runner 組建](service-source-code.md#service-source-code.build-detail)。