Help us improve the AWS re:Post Knowledge Center by sharing your feedback in a brief survey. Your input can influence how we create and update our content to better support your AWS journey.
如何在我的 Amazon MWAA 環境中安裝 Python 程式庫?
我想在 Amazon Managed Workflows for Apache Airflow (Amazon MWAA) 環境中安裝 Python 程式庫。
簡短描述
Amazon MWAA 提供公有網路和私有網路 Web 伺服器存取模式。
若要透過私有 Web 伺服器在 Amazon MWAA 環境中安裝 Python 相依性,請使用 Python wheel .whl。
若要在 Amazon MWAA 環境中安裝 Python 程式庫,請使用 requirements.txt 或 plugins.zip。當您使用 requirements.txt 時,pip 會預設安裝 Python Package Index (PyPI) 中列出的套件。如果您安裝自訂程式庫或套件,並將編譯的成品作為 .whl 檔案,請使用 plugins.zip 檔案。安裝自訂 Amazon MWAA 作業、勾點、感應器或介面時,必須使用 plugins.zip 檔案。該外掛程式匯出環境變數、驗證和組態檔,如.crt和.yaml。
解決方法
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時遇到錯誤,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
在開始之前,請確定您已滿足先決條件。
設定您的 Amazon MWAA 本機環境
請完成下列步驟:
-
使用 AWS CLI 建置 Docker 映像檔,然後設定 Amazon MWAA 本機環境。
**注意:**Amazon MWAA 儲存庫提供 AWS CLI 公用程式,並在本機複製 Amazon MWAA 環境。如需詳細資訊,請參閱 GitHub 網站上的 aws-mwaa-local-runner。
-
將 Python 程式庫和相依性新增至 requirements.txt 檔案。
**注意:**下載外掛程式後,請確定 requirements.txt 檔案包含限制條件。 -
使用以下指令碼測試 requirements.txt 檔案:
#aws-mwaa-local-runner % ./mwaa-local-env test-requirements預期輸出:
Installing requirements.txtCollecting aws-batch (from -r /usr/local/airflow/dags/requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/5d/11/3aedc6e150d2df6f3d422d7107ac9eba5b50261cf57ab813bb00d8299a34/aws_batch-0.6.tar.gz Collecting awscli (from aws-batch->-r /usr/local/airflow/dags/requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/07/4a/d054884c2ef4eb3c237e1f4007d3ece5c46e286e4258288f0116724af009/awscli-1.19.21-py2.py3-none-any.whl (3.6MB) 100% |████████████████████████████████| 3.6MB 365kB/s ... ... ... Installing collected packages: botocore, docutils, pyasn1, rsa, awscli, aws-batch Running setup.py install for aws-batch ... done Successfully installed aws-batch-0.6 awscli-1.19.21 botocore-1.20.21 docutils-0.15.2 pyasn1-0.4.8 rsa-4.7.2
如需詳細資訊,請參閱使用 PyPi.org 要求檔案格式安裝 Python 相依性。
從 requirements.txt 檔案建立 .whl 檔案
請完成下列步驟:
-
對於套件要求,請執行以下 local-runner 命令:
#aws-mwaa-local-runner % ./mwaa-local-env package-requirements**注意:此命令會將所有 .whl 檔案下載到 aws-mwaa-local-runner/plugin 資料夾中。執行 package-requirements 命令後,**plugins.zip、新的 packaged_requirements.txt 和 constraints.txt 檔案將在應用程式的 requirement/ 目錄中可用。
-
下載 plugins.zip、新的 packaged_requirements.txt 和 constraints.txt 檔案。
-
修改新的 packaged_requirements.txt 檔案,使其指向 /usr/local/airflow/dags/constraints.txt 而不是公有限制條件。
上傳 constraints.txt 檔案
您可以將 contraints.txt 檔案上傳到 Amazon Simple Storage Service (Amazon S3) 儲存貯體 dag/ 目錄,也可以將 restrictions.txt 檔案新增至 plugins 檔案中。如果您將 contraints.txt 檔案上傳到 Amazon S3 儲存貯體 dag/ 目錄,Amazon S3 就會修改 requirements.txt 檔案以進行部署。不過,如果您在 plugins 檔案中包含 constraints.txt 檔案,請確定 packaged_requirements.txt 檔案指向 /usr/local/airflow/dags/constraints.txt,而不是指向公有限制條件。
如果您將 constraints.txt 檔案上傳至外掛程式,請使用下列命令:
#aws-mwaa-local-runner % zip -j requirements/plugins.zip constraints.txt
建立新的 requirements.txt 檔案,該檔案指向已封裝在 plugins.zip 檔案中的 .whl 檔案
請完成下列步驟:
-
修改新的 packaged_requirements.txt 檔案。如果您已將檔案上傳至 Amazon S3 dags 目錄,請將下列範例中的 {OPTION} 替換為 **dags。**或者,如果您已將該檔案新增至 plugins.zip 檔案中,請將 {OPTION} 替換為 plugins:
--find-links /usr/local/airflow/plugins --no-index --constraint "/usr/local/airflow/{OPTION}/constraints.txt" ....snip..... -
將 plugins.zip 檔案和 requirements.txt 檔案上傳到 Amazon MWAA 叢集的 Amazon S3 儲存貯體。
-
更新環境。
對安裝套件進行疑難排解
使用 aws-mwaa-local-runner 測試 DAG、自訂外掛程式和 Python 相依性。從 Amazon MWAA Worker 或 Scheduler 日誌群組查看日誌檔案。
**重要:**在安裝套件或外掛程式 .zip 檔案之前,請使用 Amazon MWAA CLI 公用程式測試 Python 相依性和 plugins.zip 檔案。
相關資訊
- 語言
- 中文 (繁體)

相關內容
- 已提問 1 年前
- 已提問 1 年前
- 已提問 1 年前