Comment résoudre l'erreur « ImportError : Aucun module nommé » dans AWS Glue ?

Lecture de 3 minute(s)
0

Lorsque j'essaie d'importer des modules ou des packages supplémentaires en utilisant le shell Python AWS Glue, j'obtiens la réponse « ImportError : Aucun module nommé ». Par exemple : ImportError : Aucun module nommé pyarrow.compat

Brève description

Le shell Python AWS Glue utilise les fichiers .egg et .whl. Python peut effectuer des importations directement à partir d'un fichier .egg ou .whl. Pour maintenir la compatibilité, assurez-vous que votre environnement de génération local utilise la même version Python que la tâche shell Python. Par exemple, si vous créez un fichier .egg avec Python 3, utilisez Python 3 pour la tâche shell Python AWS Glue.

Remarque : à compter du 1er juin 2022, les tâches shell Python ne prendront en charge que Python 3. Pour plus d'informations, consultez politique d’assistance de version d'AWS Glue.

Solution

1.    Créez le fichier setup.py et ajoutez le paramètre install_requires pour répertorier les modules que vous souhaitez importer :

from setuptools import setup

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

2.    Créez un dossier nommé reshift_module dans le répertoire actuel :

$ mkdir redshift_module

Puis, installez les packages :

$ python setup.py develop

Exemple de sortie :

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.    Effectuez l'une des actions suivantes.

Créez un fichier .egg :

python setup.py bdist_egg

-ou- Créer un fichier .whl :

python setup.py bdist_wheel

5.    Copiez le fichier .egg ou .whl du dossier dist vers un compartiment Amazon Simple Storage Service (Amazon S3). Pour plus d'informations, consultez Fourniture de votre propre bibliothèque Python. Exemple :

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

Remarque : si vous recevez des erreurs lors de l'exécution de commandes depuis l'interface de ligne de commande AWS (AWS CLI), assurez-vous que vous utilisez la version la plus récente d'AWS CLI.

6.    Le module est maintenant installé dans votre tâche de shell Python. Pour confirmer, consultez le groupe Amazon CloudWatch Logs dédié aux tâches de shell Python (/aws-glue/python-jobs/output). Voici un exemple d'une sortie réussie :

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

Informations connexes

Comment puis-je utiliser des bibliothèques Python externes dans ma tâche ETL AWS Glue 1.0 ou 0.9 ?

Comment puis-je utiliser des bibliothèques Python externes dans ma tâche ETL AWS Glue 2.0 ?

AWS OFFICIEL
AWS OFFICIELA mis à jour il y a 2 ans