如何解决 AWS Glue 中的“ImportError: No module named”(导入错误:没有命名模块)?

上次更新时间:2020 年 12 月 16 日

当我尝试使用 AWS Glue Python shell 导入额外的模块或软件包时,我收到“ImportError: No module named”(导入错误:没有命名模块)响应。例如:

ImportError: No module named pyarrow.compat(导入错误:没有名为 pyarrow.compat 的模块)

简短描述

AWS Glue Python shell 使用 .egg.whl 文件。Python 可以直接从 .egg.whl 文件导入。为了保持兼容性,请确保本地构建环境使用与 Python shell 作业相同的 Python 版本。例如,如果您使用 Python 2.7 构建 .egg 文件,请将 Python 2.7 用于 AWS Glue Python shell 作业。

解决方法

1.创建 setup.py 文件并添加 install_requires 参数以列出要导入的模块:

from setuptools import setup

setup(
    name="redshift_module",
    version="0.1",
    packages=,
    install_requires=
)

2.安装软件包:

$ python2 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/python2.7/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/2.7/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/python2.7/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 文件:

python2 setup.py bdist_egg

-或- 创建一个 .whl 文件:

python setup.py bdist_wheel

5.将 .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 命令行界面 (AWS CLI) 命令时遇到错误,请确保您使用的是最新版本的 AWS CLI。

6.该模块现在已安装在你的 Python shell 作业中。要确认,请检查 Amazon CloudWatch Logs 组以获取 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-py2.7-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-py2.7-linux-x86_64.egg