我該如何解決 AWS Glue 中的「ImportError: No module named」?

2 分的閱讀內容
0

當我嘗試使用 AWS Glue Python Shell 匯入額外的模組或套件時,收到「ImportError: No module named」。例如: ImportError: 沒有名為 pyarrow.compat 的模組

簡短描述

AWS Glue Python Shell 使用 .egg.whl 檔案。Python 可以直接從 .egg.whl 檔案匯入。為保持相容性,請確定您的本機建置環境使用與 Python Shell 作業相同的 Python 版本。例如,如果您使用 Python 3 構建 .egg 檔案,請使用 Python 3 進行 AWS Glue Python Shell 作業。

**注意:**自 2022 年 6 月 1 日起,Python Shell 作業僅支援 Python 3。如需詳細資訊,請參閱 AWS Glue 版本支援政策

解決方法

  1. 建立 setup.py 檔案並新增 install_requires 參數以列出要匯入的模組:
from setuptools import setup

setup(
    name="redshift_module",
    version="0.1",
    packages=['redshift_module'],
    install_requires=['pyarrow','pandas','numpy','fastparquet']
)
  1. 在目前的目錄下建立一個名為 reshift_module 的資料夾:
$ mkdir redshift_module

然後,安裝套件:

$ python setup.py develop

範例輸出:

running develop
running egg_info
writing requirements to redshift_module.egg-info/requires.txt
writing redshift_module.egg-info/PKG-INFO
writing top-level names to redshift_module.egg-info/top_level.txt
writing dependency_links to redshift_module.egg-info/dependency_links.txt
reading manifest file 'redshift_module.egg-info/SOURCES.txt'
writing manifest file 'redshift_module.egg-info/SOURCES.txt'
running build_ext
Creating /usr/local/lib/python3.6/site-packages/redshift-module.egg-link (link to .)
redshift-module 0.1 is already the active version in easy-install.pth
Using /Users/test/Library/Python/3.6/lib/python/site-packages
Searching for pandas==0.24.2
Best match: pandas 0.24.2
Adding pandas 0.24.2 to easy-install.pth file

Using /usr/local/lib/python3.6/site-packages
Searching for pyarrow==0.12.1
Best match: pyarrow 0.12.1
Adding pyarrow 0.12.1 to easy-install.pth file
Installing plasma_store script to /usr/local/bin
  1. 執行下列任一項作業:

建立 .egg 檔案:

python setup.py bdist_egg

或建立 .whl 檔案:

python setup.py bdist_wheel
  1. .egg.whl 檔案從 dist 資料夾複製到 Amazon Simple Storage Service (Amazon S3) 儲存貯體。如需詳細資訊,請參閱提供您自己的 Python 程式庫。範例:
dist aws s3 cp MOA_EDM_cdc_controller_g2-0.2.9-py3-none-any.whl s3://doc-example-bucket/glue-libs/python-shell-jobs/
upload: ./MOA_EDM_cdc_controller_g2-0.2.9-py3-none-any.whl to s3://doc-example-bucket/glue-libs/python-shell-jobs/MOA_EDM_cdc_controller_g2-0.2.9-py3-none-any.whl

**注意:**如果在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請確定您使用的是最新版本的 AWS CLI

  1. 該模組現在已安裝在您的 Python Shell 作業中。若要確認,請檢查 Amazon CloudWatch 日誌群組是否有 Python Shell 作業 (/aws-glue/python-jobs/output)。以下是成功輸出的範例:
Searching for pyarrow
Reading https://pypi.python.org/simple/pyarrow/
Downloading https://files.pythonhosted.org/packages/fe/3b/267c0fdb3dc5ad7989417cfb447fbcbec008bafc1bb26d4f0221c5e4e508/pyarrow-0.12.1-cp27-cp27mu-manylinux1_x86_64.whl#sha256=63170571cccaf0bf01a1d30eacc4d9274bd5c4f448c2b5b1a4ddc125952f4284
Best match: pyarrow 0.12.1
Processing pyarrow-0.12.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing pyarrow-0.12.1-cp27-cp27mu-manylinux1_x86_64.whl to /glue/lib/installation
writing requirements to /glue/lib/installation/pyarrow-0.12.1-py3.6-linux-x86_64.egg/EGG-INFO/requires.txt
Adding pyarrow 0.12.1 to easy-install.pth file
Installing plasma_store script to /glue/lib/installation
Installed /glue/lib/installation/pyarrow-0.12.1-py3.6-linux-x86_64.egg

相關資訊

如何在 AWS Glue 1.0 或 0.9 ETL 作業中使用外部 Python 程式庫?

如何在 AWS Glue 2.0 ETL 作業中使用外部 Python 程式庫?

AWS 官方
AWS 官方已更新 2 年前