python - 无法导入模块 'lambda_function' : No module named 'cx_Oracle'

标签 python amazon-web-services aws-lambda

我正在尝试使用 Python 创建一个 AWS Lambda 函数来连接到 Oracle 数据库(目前只是测试连接)。但我没有成功完成流程。每次我看到此错误消息时:


    {
      "errorMessage": "Unable to import module 'lambda_function': No module named 'cx_Oracle'",
      "errorType": "Runtime.ImportModuleError"
    }

I've created a virtualenv at Ubuntu WSL, install the Oracle InstantClient on the lib folder, install cx_oracle by pip at sites-package folder, and create my lambda function at the same folder, zip everything, upload at my S3 and put to run.

Can anyone help me?

My code:

    import cx_Oracle

    # Yeah, you need this
    with open('/tmp/HOSTALIASES', 'w') as f: f.write(f'{os.uname()[1]} localhost\n')

    # Oracle away!
    def lambda_handler(event, context):
        return str(
            cx_Oracle.connect(
                'username',
                'password',
                cx_Oracle.makedsn(
                    'rds.amazonaws.com', 1521, 'SOME_SID',
                )
            ).cursor().execute('SELECT 42 FROM DUAL').fetchone()
        )

我的网站包文件夹: folder

** 我的 lambda 配置:** lambda

最佳答案

AWS Lambda 与 Amazon Linux 一起运行,因此您需要 cx_Oracle 的兼容库,在本例中 manylinux.whl

只需下载 whl,解压缩,然后将 .so 文件复制到您的 lib 文件夹中。

这样 Lambda 将识别 cx_Oracle。

文件夹结构应如下所示: structure

关于python - 无法导入模块 'lambda_function' : No module named 'cx_Oracle' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56996655/

相关文章:

python - 运行dockerized Hello world python示例时执行格式错误

python - yaml.dump 在多行字符串中添加不需要的换行符

ios - 是否可以安全访问存储在 AWS 上的图像?

node.js - 如何用 Jest 模拟 AWS Cognito CognitoIdentityServiceProvider?

amazon-web-services - 根据 Cloudformation 堆栈更新/完成事件触发 lambda

python - 使用 Splash Scrapy POST 到 Javascript 生成的表单

python - 在与以前工作正常的完全相同的代码上收到 "pytesseract not in your path"错误

amazon-web-services - 当文件中明确指定了 executionRoleArn 时,为什么我会收到有关未指定 executionRoleArn 的错误?

javascript - 将节点请求模块与 AWS Lambda 和 API Gateway 结合使用

node.js - 在 NodeJS 中使用 Lambda@Edge 对 CloudFront 进行基本 HTTP 身份验证