Como resolvo “ImportError: Nenhum módulo nomeado” no AWS Glue?

3 minuto de leitura
0

Quando tento importar módulos ou pacotes extras usando o shell Python do AWS Glue, recebo uma resposta “ImportError: Nenhum módulo nomeado”. Por exemplo: ImportError: Nenhum módulo chamado pyarrow.compat

Descrição breve

O shell Python do AWS Glue usa arquivos .egg e .whl. O Python pode importar diretamente de um arquivo .egg ou .whl. Para manter a compatibilidade, certifique-se de que seu ambiente de compilação local use a mesma versão Python que o trabalho de shell Python. Por exemplo, se você criar um arquivo .egg com o Python 3, use o Python 3 para o trabalho de shell Python do AWS Glue.

Observação: a partir de 1º de junho de 2022, os trabalhos de shell do Python oferecem suporte somente ao Python 3. Para mais informações, consulte a Política de suporte a versões do AWS Glue.

Resolução

1.    Crie o arquivo setup.pye adicione o parâmetro install_requires para listar os módulos que você deseja importar:

from setuptools import setup

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

2.    Crie uma pasta chamada reshift_module no diretório atual:

$ mkdir redshift_module

Em seguida, instale os pacotes:

$ python setup.py develop

Saída de exemplo:

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.    Execute um destes procedimentos:

Crie um arquivo .egg:

python setup.py bdist_egg

-ou- Crie um arquivo .whl:

python setup.py bdist_wheel

5.    Copie o arquivo .egg ou .whl da pasta dist para um bucket do Amazon Simple Storage Service (Amazon S3). Para mais informações, consulte Fornecer sua própria biblioteca Python. Exemplo:

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

Observação: se você receber erros ao executar comandos da AWS Command Line Interface (AWS CLI), certifique-se de estar usando a versão mais recente da AWS CLI.

6.    O módulo agora está instalado em seu trabalho de shell Python. Para confirmar, verifique o grupo do Amazon CloudWatch Logs em busca de trabalhos de shell Python (/aws-glue/python-jobs/output). Aqui está um exemplo de uma saída bem-sucedida:

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

Informações relacionadas

Como usar bibliotecas Python externas no meu trabalho de ETL do AWS Glue 1.0 ou 0.9?

Como usar bibliotecas Python externas no meu trabalho de ETL do AWS Glue 2.0?

AWS OFICIAL
AWS OFICIALAtualizada há 2 anos