AWS Glue で「ImportError: モジュール名が指定されていません」エラーを解決する方法を教えてください。

所要時間2分
0

AWS Glue Python シェルを使用して追加のモジュールまたはパッケージをインポートしようとすると、「ImportError: モジュール名が指定されていません」エラーが表示されます。例: ImportError: pyarrow.compat という名前のモジュールはありません

簡単な説明

AWS Glue Python シェルは、.egg ファイルおよび .whl ファイルを使用します。Python は、.egg ファイルまたは .whl ファイルから直接インポートできます。互換性を維持するには、ローカルビルド環境で Python シェルジョブと同じ Python バージョンを使用してください。例えば、Python 3 で .egg ファイルを構築する場合、AWS Glue Python シェルジョブに Python 3 を使用してください。

注: 2022 年 6 月 1 日以降、Python シェルジョブでは Python 3 のみがサポートされています。詳細については、AWS Glue バージョンサポートポリシーを参照してください。

解決方法

1.    setup.py ファイルを作成し、install_recires パラメータを追加して、インポートするモジュールをリストします。

from setuptools import setup

setup(
    name="redshift_module",
    version="0.1",
    packages=['redshift_module'],
    install_requires=['pyarrow','pandas','numpy','fastparquet']
)

2.    現在のディレクトリの下に 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

3.    次のいずれかを行ってください。

.egg ファイルを作成する:

python setup.py bdist_egg
  • または - .whl ファイルを作成する:
python setup.py bdist_wheel

5.    .egg ファイルまたは .whl ファイルを dit フォルダから 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 コマンドラインインターフェイス (AWS CLI) コマンドの実行中にエラーが発生した場合は、最新の AWS CLI バージョンを使用していることを確認してください

6.    これで、モジュールが Python シェルジョブにインストールされました。確認するには、Amazon CloudWatch Logs グループで Python シェルジョブ (/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年前
コメントはありません