Runtime.ImportModuleError:无法导入模块“ lambda_function”:没有名为“ httplib2”的模块

问题描述

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

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的图层。但是,当我在本地运行pyhon文件且环境中有这样的lib时,甚至一直想起[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'httplib2',在调用import httplib2之后我没有收到任何错误。注意:其他所有库都可以在lambda上运行,因此我的上传过程应该正确。

解决方法

您可以使用包创建Lambda custom layer

要检查此解决方案,我创建了一个这样的层,可以确认其有效

所使用的技术包括最近的 AWS博客中所述的 docker工具

因此,对于这个问题,我验证了,如下所示:

  1. 创建空文件夹,例如mylayer

  2. 转到该文件夹​​并创建内容为{p>的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
  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"
  1. 以zip格式创建图层:
zip -r -9 mylayer.zip python 
  1. 在AWS控制台中基于mylayer.zip创建lambda层。 不要忘记为Compatible runtimes指定python3.8

  2. 使用以下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']