

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

# 為 Apache Airflow PythonVirtualenvOperator 建立自訂外掛程式
<a name="samples-virtualenv"></a>

下列範例說明如何在 Amazon Managed Workflows for Apache Airflow 上使用`PythonVirtualenvOperator`自訂外掛程式修補 Apache Airflow。

**Topics**
+ [版本](#samples-virtualenv-version)
+ [先決條件](#samples-virtualenv-prereqs)
+ [權限](#samples-virtualenv-permissions)
+ [要求](#samples-virtualenv-dependencies)
+ [自訂外掛程式範例程式碼](#samples-virtualenv-plugins-code)
+ [Plugins.zip](#samples-virtualenv-pluginszip)
+ [範例程式碼](#samples-virtualenv-code)
+ [Airflow 組態選項](#samples-virtualenv-airflow-config)
+ [後續步驟？](#samples-virtualenv-next-up)

## 版本
<a name="samples-virtualenv-version"></a>

您可以使用此頁面上的程式碼範例搭配 Python 3.10 中的 **Apache Airflow v2** 和 Python 3.11 中的 **Apache Airflow v**3。 [https://peps.python.org/pep-0619/](https://peps.python.org/pep-0619/) [https://peps.python.org/pep-0664/](https://peps.python.org/pep-0664/)

## 先決條件
<a name="samples-virtualenv-prereqs"></a>

若要使用此頁面上的範例程式碼，您需要下列項目：
+ [Amazon MWAA 環境](get-started.md)。

## 權限
<a name="samples-virtualenv-permissions"></a>

使用此頁面上的程式碼範例不需要額外的許可。

## 要求
<a name="samples-virtualenv-dependencies"></a>

若要使用此頁面上的範例程式碼，請將下列相依性新增至您的 `requirements.txt`。若要進一步了解，請參閱 [安裝 Python 相依性](working-dags-dependencies.md)。

```
virtualenv
```

## 自訂外掛程式範例程式碼
<a name="samples-virtualenv-plugins-code"></a>

Apache Airflow 會在啟動時執行外掛程式資料夾中 Python 檔案的內容。此外掛程式會在該啟動程序`PythonVirtualenvOperator`期間修補內建 ，使其與 Amazon MWAA 相容。下列步驟顯示自訂外掛程式的範例程式碼。

1. 在您的命令提示字元中，導覽至上一節中的 `plugins` 目錄。例如：

   ```
   cd plugins
   ```

1. 複製下列程式碼範例的內容，並在本機儲存為 `virtual_python_plugin.py`。

   ```
   """
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
    
   Permission is hereby granted, free of charge, to any person obtaining a copy of
   this software and associated documentation files (the "Software"), to deal in
   the Software without restriction, including without limitation the rights to
   use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
   the Software, and to permit persons to whom the Software is furnished to do so.
    
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
   FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
   COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   """
   from airflow.plugins_manager import AirflowPlugin
   import airflow.utils.python_virtualenv 
   from typing import List
   
   def _generate_virtualenv_cmd(tmp_dir: str, python_bin: str, system_site_packages: bool) -> List[str]:
       cmd = ['python3','/usr/local/airflow/.local/lib/python3.7/site-packages/virtualenv', tmp_dir]
       if system_site_packages:
           cmd.append('--system-site-packages')
       if python_bin is not None:
           cmd.append(f'--python={python_bin}')
       return cmd
   
   airflow.utils.python_virtualenv._generate_virtualenv_cmd=_generate_virtualenv_cmd
   
   class VirtualPythonPlugin(AirflowPlugin):                
       name = 'virtual_python_plugin'
   ```

## Plugins.zip
<a name="samples-virtualenv-pluginszip"></a>

下列步驟說明如何建立 `plugins.zip`。

1. 在您的命令提示字元中，導覽至上一節`virtual_python_plugin.py`中包含 的目錄。例如：

   ```
   cd plugins
   ```

1. 壓縮`plugins`資料夾中的內容。

   ```
   zip plugins.zip virtual_python_plugin.py
   ```

## 範例程式碼
<a name="samples-virtualenv-code"></a>

下列步驟說明如何建立自訂外掛程式的 DAG 程式碼。

1. 在命令提示中，導覽至存放 DAG 程式碼的目錄。例如：

   ```
   cd dags
   ```

1. 複製下列程式碼範例的內容，並在本機儲存為 `virtualenv_test.py`。

   ```
   """
   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
    
   Permission is hereby granted, free of charge, to any person obtaining a copy of
   this software and associated documentation files (the "Software"), to deal in
   the Software without restriction, including without limitation the rights to
   use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
   the Software, and to permit persons to whom the Software is furnished to do so.
    
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
   FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
   COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   """
   from airflow import DAG
   from airflow.operators.python import PythonVirtualenvOperator
   from airflow.utils.dates import days_ago
   import os
   
   os.environ["PATH"] = os.getenv("PATH") + ":/usr/local/airflow/.local/bin"
   
   def virtualenv_fn():
       import boto3
       print("boto3 version ",boto3.__version__)
   
   with DAG(dag_id="virtualenv_test", schedule_interval=None, catchup=False, start_date=days_ago(1)) as dag:
       virtualenv_task = PythonVirtualenvOperator(
           task_id="virtualenv_task",
           python_callable=virtualenv_fn,
           requirements=["boto3>=1.17.43"],
           system_site_packages=False,
           dag=dag,
       )
   ```

## Airflow 組態選項
<a name="samples-virtualenv-airflow-config"></a>

如果您使用的是 Apache Airflow v2，請將 新增`core.lazy_load_plugins : False`為 Apache Airflow 組態選項。若要進一步了解，請參閱[使用組態選項載入 2 中的外掛程式](configuring-env-variables.md#configuring-2.0-airflow-override)。

## 後續步驟？
<a name="samples-virtualenv-next-up"></a>
+ 了解如何在此範例中將`requirements.txt`檔案上傳至 中的 Amazon S3 儲存貯體[安裝 Python 相依性](working-dags-dependencies.md)。
+ 了解如何在此範例中將 DAG 程式碼上傳至 Amazon S3 儲存貯體中的 `dags` 資料夾[新增或更新 DAGs](configuring-dag-folder.md)。
+ 進一步了解如何在此範例中將`plugins.zip`檔案上傳至 中的 Amazon S3 儲存貯體[安裝自訂外掛程式](configuring-dag-import-plugins.md)。