AWS CodeBuild报错:找不到generatedata.py文件的解决求助
问题:AWS CodeBuild构建失败,提示找不到generatedata.py文件
尝试通过AWS CodeBuild执行Python脚本生成Excel文件并上传至S3存储桶,但构建过程报错找不到generatedata.py文件。已确认仓库名称及源路径正确,寻求问题修正方案。
相关代码文件
generatedata.py
import pandas as pd import numpy as np # Generate sample data data = { 'Name': ['Alice','Bob','Charlie','David','Emma'], 'Age': np.random.randint(20,40, size=5), 'Salary': np.random.randint(30000,80000, size=5) } # Create a dataframe df = pd.DataFrame(data) # Perform some data manipulation df['Bonus'] = df['Salary'] * 0.1 # Display the Dataframe print("Original dataframe:") print(df) # Store the data in an excel file excel_filename = 'sample_data.xlsx' df.to_excel(excel_filename, index=False) print(f"\nData saved to '{excel_filename}' successfully")
buildspec.yml
version: 0.2 phases: install: runtime-versions: python: 3.11 pre_build: commands: - echo "Installing dependencies" - pip install pandas numpy build: commands: - echo "Running the script" - data-analysis/generatedata.py - aws s3 cp sample_data.xlsx s3://upload-data-files-to-s3/
问题分析与修正方案
1. 脚本执行方式错误(核心问题)
当前build阶段直接运行data-analysis/generatedata.py,存在两个问题:
- 未指定Python解释器,系统无法识别这是Python脚本
- 脚本缺少可执行权限及shebang头(若要直接运行脚本)
修正方案(推荐):用Python解释器执行脚本
修改buildspec.yml的build阶段命令,添加python前缀:
build: commands: - echo "Running the script" - python data-analysis/generatedata.py - aws s3 cp sample_data.xlsx s3://upload-data-files-to-s3/
2. 可选:验证脚本路径正确性
若仍报错,可添加路径检查命令确认文件是否存在:
build: commands: - echo "Checking script directory" - ls -la data-analysis/ # 查看data-analysis目录下的文件 - echo "Running the script" - python data-analysis/generatedata.py - aws s3 cp sample_data.xlsx s3://upload-data-files-to-s3/
3. 额外补全:Excel依赖缺失
pandas.to_excel需要openpyxl引擎支持,当前pre_build阶段未安装,脚本运行会触发另一个错误,需补充安装:
pre_build: commands: - echo "Installing dependencies" - pip install pandas numpy openpyxl
内容的提问来源于stack exchange,提问作者1111
相关产品推荐
相关产品推荐

