AWS Glue 3.0 PythonShell依赖版本不兼容问题求助
问题描述
我在构建数据湖ETL与分析项目,使用AWS S3存储数据、Glue运行任务、Crawler+Athena创建数据表,当前遇到CloudWatch日志显示的依赖版本不兼容错误:
scipy 1.8.0 requires numpy<1.25.0,>=1.17.3, but you have numpy 2.0.2 which is incompatible.
redshift-connector 2.0.907 requires pytz<2022.2,>=2020.1, but you have pytz 2025.2 which is incompatible.
awswrangler 2.15.1 requires numpy<2.0.0,>=1.21.0, but you have numpy 2.0.2 which is incompatible.
awswrangler 2.15.1 requires pandas<2.0.0,>=1.2.0, but you have pandas 2.2.3 which is incompatible.
awscli 1.23.5 requires botocore==1.25.5, but you have botocore 1.38.9 which is incompatible.
awscli 1.23.5 requires s3transfer<0.6.0,>=0.5.0, but you have s3transfer 0.12.0 which is incompatible.
aiobotocore 2.2.0 requires botocore<1.24.22,>=1.24.21, but you have botocore 1.38.9 which is incompatible.
使用Glue 3.0 PythonShell模式,项目包打包成.whl存储在S3依赖目录,主脚本main.py位于S3脚本目录,创建任务的代码片段如下:
Name=config_data["aws_glue"]["etl_jobs"][0]["name"], Role=glue_arn, # Replace with your IAM role ARN JobMode='SCRIPT', ExecutionProperty={'MaxConcurrentRuns': 1}, Command={ 'Name': 'pythonshell', 'ScriptLocation': config_data["aws_glue"]["S3_URI"], 'PythonVersion': '3.9' }, DefaultArguments={ '--TempDir': str(config_data["s3_bucket"]["bucket"] + config_data["s3_bucket"]["temp"]), '--extra-py-files': str(config_data["s3_bucket"]["bucket"] + config_data["s3_bucket"]["dependencies"] + 'pokemon_datalake_and_anltx-0.1.0-cp39-none-any.whl'), '--job-language': 'python' }, MaxRetries=0, GlueVersion='3.0', Description='Job for processing raw Pokémon data.' ) print(f"Job has been created: {response['Name']}")
未使用--additional-python-modules参数,之前尝试添加boto3和pandas仍报错,任务启动几秒后失败,requirements.txt已明确包含所有AWS Glue 3.0 Python模块,求问题原因及解决方法。
问题根源
- Glue预装依赖与自定义包冲突:Glue 3.0 PythonShell环境本身预装了特定版本的核心依赖(如botocore、numpy、pandas等),自定义
.whl中包含的高版本/不兼容依赖覆盖了预装版本,引发冲突。 - whl打包未排除预装模块:自定义whl将Glue已自带的模块也打包进去,导致环境中出现重复且版本不符的依赖。
- 第三方库版本未匹配Glue基线:引入的第三方库(如awswrangler、redshift-connector)版本要求与Glue预装依赖版本不兼容,且未在打包时做版本对齐。
具体解决步骤
1. 清理自定义whl,排除Glue预装模块
Glue 3.0 PythonShell预装的核心模块及版本:
- botocore==1.25.5
- numpy==1.21.6
- pandas==1.3.5
- pytz==2021.3
- boto3==1.22.5
- awscli==1.23.5
修改打包配置(如setup.py或pyproject.toml),仅保留Glue未预装的模块,或指定兼容版本的第三方库:
# setup.py示例 setup( name="pokemon_datalake_and_anltx", version="0.1.0", # ...其他基础配置 install_requires=[ # 只保留Glue没有的模块,或指定兼容预装版本的第三方库 "awswrangler==2.15.1", # 兼容numpy<2.0.0,匹配Glue的numpy 1.21.6 "redshift-connector==2.0.907", # 兼容pytz<2022.2,匹配Glue的pytz 2021.3 "scipy==1.8.0" # 兼容numpy<1.25.0,匹配Glue的numpy 1.21.6 ] )
重新打包whl,确保仅包含自定义代码和兼容的第三方库。
2. 调整Glue任务参数,避免覆盖预装依赖
改用--additional-python-modules指定需要安装的第三方库,让Glue在预装环境基础上自动处理依赖兼容;如果仅需上传自定义代码,用--extra-py-files传纯代码压缩包:
DefaultArguments={ '--TempDir': str(config_data["s3_bucket"]["bucket"] + config_data["s3_bucket"]["temp"]), '--additional-python-modules': 'awswrangler==2.15.1,redshift-connector==2.0.907,scipy==1.8.0', '--job-language': 'python', # 仅上传自定义代码(不含依赖) '--extra-py-files': str(config_data["s3_bucket"]["bucket"] + config_data["s3_bucket"]["dependencies"] + 'custom_code_only.zip') },
3. 严格对齐requirements.txt与Glue版本
更新requirements.txt,明确标注Glue预装模块版本及兼容的第三方库:
# Glue 3.0预装模块,无需安装,仅做版本声明 botocore==1.25.5 numpy==1.21.6 pandas==1.3.5 pytz==2021.3 awscli==1.23.5 boto3==1.22.5 # 第三方库,指定兼容上述版本的版本号 awswrangler==2.15.1 redshift-connector==2.0.907 scipy==1.8.0
4. 本地验证依赖兼容性
创建Python 3.9虚拟环境,模拟Glue 3.0环境测试依赖是否冲突:
# 创建虚拟环境 python3.9 -m venv glue3-env source glue3-env/bin/activate # 安装Glue 3.0核心依赖 pip install botocore==1.25.5 numpy==1.21.6 pandas==1.3.5 pytz==2021.3 awscli==1.23.5 # 安装第三方库 pip install awswrangler==2.15.1 redshift-connector==2.0.907 scipy==1.8.0 # 检查依赖冲突 pip check
若pip check无报错,说明依赖组合兼容,再部署到Glue。
内容的提问来源于stack exchange,提问作者Cecelia

