Come posso risolvere il problema "ImportError: No module named" in AWS Glue?

3 minuti di lettura
0

Quando cerco di importare moduli o pacchetti aggiuntivi utilizzando la shell Python di AWS Glue, si verifica un errore "ImportError: No module named". Ad esempio: ImportError: No module named pyarrow.compat

Breve descrizione

La shell (interprete di comandi) Python di AWS Glue utilizza file .egg e .whl. Python può importare direttamente da un file .egg o .whl. Per mantenere la compatibilità, assicurati che l’ambiente di compilazione locale utilizzi la stessa versione di Python del processo della shell Python. Ad esempio, se crei un file .egg con Python 3, usa Python 3 per il processo della shell Python di AWS Glue.

Nota: a partire dal 1 giugno 2022, i processi della shell Python supportano solo Python 3. Per ulteriori informazioni, consulta la AWS Glue version support policy.

Risoluzione

1.    Crea il file setup.py e aggiungi il parametro install_requires per elencare i moduli che desideri importare:

from setuptools import setup

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

2.    Crea una cartella denominata reshift_module nella directory corrente:

$ mkdir redshift_module

Quindi, installa i pacchetti:

$ python setup.py develop

Esempio di output:

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.    Effettua una delle seguenti operazioni:

Crea un file .egg:

python setup.py bdist_egg

-oppure- Crea un file .whl:

python setup.py bdist_wheel

5.    Copia il file .egg o .whl dalla cartella dist in un bucket Amazon Simple Storage Service (Amazon S3). Per ulteriori informazioni, consulta Providing your own Python library. Esempio:

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

Nota: se si verificano errori durante l'esecuzione dei comandi dell’interfaccia della linea di comando AWS (AWS CLI), assicurati di utilizzare la versione più recente di AWS CLI.

6.    Il modulo è ora installato nel processo della shell Python. Per confermare, controlla i processi della shell Python nel gruppo Amazon CloudWatch Logs (/aws-glue/python-jobs/output). Ecco un esempio di output corretto:

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

Informazioni correlate

How do I use external Python libraries in my AWS Glue 1.0 or 0.9 ETL job?

How do I use external Python libraries in my AWS Glue 2.0 ETL job?

AWS UFFICIALE
AWS UFFICIALEAggiornata 2 anni fa