python - Runtime.ImportModuleError : Unable to import module 'lambda_function' : No module named 'httplib2'

标签 python amazon-web-services aws-lambda google-api aws-lambda-layers

我已经安装了所有这些库:

google-api-core 1.22.1
google-api-python-client 1.10.0
google-auth 1.20.1
google-auth-httplib2 0.0.4
google-auth-oauthlib 0.4.1
google-cloud 0.34.0
google-cloud-core 1.4.1
google-cloud-storage 1.31.0
google-crc32c 1.0.0
google-resumable-media 1.0.0
googleapis-common-protos 1.52.0
httplib2 0.18.1
到一个文件夹 --> 压缩它 --> 上传到 AWS lambda 的层。但是,我不断收到[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'httplib2'甚至认为当我在本地运行我的 pyhon 文件并且环境中存在这样的库时,我在调用 import httplib2 后没有收到任何错误。 .注意:所有其他库都适用于 lambda,所以我的上传过程应该是正确的。

最佳答案

您可以创建一个 Lambda custom layer带着你的包裹。
为了检查这个解决方案,我创建了一个这样的层,可以 确认它有效 .
使用的技术包括 码头工具最近在中描述AWS 博客 :

  • How do I create a Lambda layer using a simulated Lambda environment with Docker?

  • 因此对于这个问题,我验证了如下:
  • 创建空文件夹,例如mylayer .
  • 转到文件夹并创建 requirements.txt包含内容的文件
  • google-api-core==1.22.1
    google-api-python-client==1.10.0
    google-auth==1.20.1
    google-auth-httplib2==0.0.4
    google-auth-oauthlib==0.4.1
    google-cloud==0.34.0
    google-cloud-core==1.4.1
    google-cloud-storage==1.31.0
    google-crc32c==1.0.0
    google-resumable-media==1.0.0
    googleapis-common-protos==1.52.0
    httplib2==0.18.1
    
  • 运行以下 docker 命令:
  • docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"
    
  • 将图层创建为 zip:
  • zip -r -9 mylayer.zip python 
    
  • 基于 mylayer.zip 创建 lambda 层在 AWS 控制台中。
    不要忘记指定 Compatible runtimespython3.8 .
  • 使用以下 lambda 函数测试 lambda 中的层:
  • import httplib2
    
    def lambda_handler(event, context):
        # TODO implement
        print(dir(httplib2))
        return 
    
    
    该函数正确执行:
    ['AUTH_SCHEME_CLASSES', 'AUTH_SCHEME_ORDER', 'AllHosts', 'Authentication', 'BasicAuthentication', 'CA_CERTS', 'Credentials', 'DEFAULT_MAX_REDIRECTS', 'DEFAULT_TLS_VERSION', 'DigestAuthentication', 'FailedToDecompressContent', 'FileCache', 'GoogleLoginAuthentication', 'HOP_BY_HOP', 'HTTPConnectionWithTimeout', 'HTTPSConnectionWithTimeout', 'HmacDigestAuthentication', 'Http', 'HttpLib2Error', 'HttpLib2ErrorWithResponse', 'KeyCerts', 'MalformedHeader', 'NORMALIZE_SPACE', 'ProxiesUnavailableError', 'ProxyInfo', 'REDIRECT_CODES', 'RETRIES', 'RedirectLimit', 'RedirectMissingLocation', 'RelativeURIError', 'Response', 'SAFE_METHODS', 'SCHEME_TO_CONNECTION', 'ServerNotFoundError', 'UNQUOTE_PAIRS', 'URI', 'USE_WWW_AUTH_STRICT_PARSING', 'UnimplementedDigestAuthOptionError', 'UnimplementedHmacDigestAuthOptionError', 'WWW_AUTH_RELAXED', 'WWW_AUTH_STRICT', 'WsseAuthentication', '_', '__all__', '__author__', '__builtins__', '__cached__', '__contributors__', '__copyright__', '__doc__', '__file__', '__license__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_bind_write_headers', '_build_ssl_context', '_cnonce', '_convert_byte_str', '_decompressContent', '_entry_disposition', '_get_end2end_headers', '_md5', '_normalize_headers', '_parse_cache_control', '_parse_www_authenticate', '_sha', '_updateCache', '_wsse_username_token', 'base64', 'calendar', 'certs', 'copy', 'debuglevel', 'email', 'errno', 'gzip', 'has_timeout', 'header', 'hmac', 'http', 'io', 'iri2uri', 'os', 'parse_uri', 'proxy_info_from_environment', 'proxy_info_from_url', 'random', 're', 're_unsafe', 're_url_scheme', 'safename', 'socket', 'socks', 'ssl', 'sys', 'time', 'urllib', 'urlnorm', 'zlib']
    

    关于python - Runtime.ImportModuleError : Unable to import module 'lambda_function' : No module named 'httplib2' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63931402/

    相关文章:

    node.js - 使用 Node JS 在 Lambda 中解析 CSV

    java - 使用 Java lambda 读取 aws s3 上的文件

    python - 从 Python 脚本执行 Julia 文件

    python - 无法使用 python 获得将 .xml 文件保存到磁盘的权限

    python - Numpy/pandas 优化 : bins counting

    java - 亚马逊 aws ubuntu java mysql 网络服务 "Error calling Driver#connect"

    python-3.x - 使用预签名 URL 从 Amazon S3 读取 zip 文件,无需知道对象键和存储桶名称

    android - 从 Android Studio Gradle 构建为 AWS Device Farm 生成 UIAutomator 测试 JAR

    python - 如何使用 Pandas 指定散点图中各个点的颜色

    python - 编码 Python 列表以使用 re 模块