运行时.ImportModuleError: 无法导入模块'lambda_function': 没有名为'httplib2'的模块。

5

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

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

将文件夹压缩成zip文件,然后上传到AWS Lambda的层上。但是,当我在本地运行带有这些库的Python文件并调用import httplib2时没有出现错误,但我不断得到[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'httplib2'。注意:Lambda上的所有其他库都能正常工作,所以我的上传过程应该是正确的。


您是否可以访问 Docker,最好是在 Linux 上?我可以尝试构建一个带有这些依赖项的自定义层。但是需要在 Linux 和 Docker 上进行。 - Marcin
1
抱歉,我从未使用过Docker或Linux。 - user13888987
你只需要安装它,不需要做更多的事情。我可以测试为你的问题创建这样的层,但是我的所有层都是使用docker构建的。因此,我可以给出的任何指令都将涉及在docker中执行一个命令。 - Marcin
1
非常感谢您的帮助,@Marcin!我实际上花了一整天的时间学习Docker,以便我们可以更好地进行通信。我还可以借用我的兄弟的MacBook,使这个过程更加顺畅。我会等待您的指令 :) - user13888987
已添加答案,为您的问题创建了工作层。如果有任何问题,请告诉我。 - Marcin
1个回答

4
您可以创建一个Lambda自定义层,其中包含您的软件包。
为了验证此解决方案,我创建了这样一层,并确认它确实有效
所使用的技术包括在最近的AWS博客中描述的Docker工具 因此,对于这个问题,我验证了以下内容:
  1. 创建空文件夹,例如mylayer
  2. 进入该文件夹并创建包含以下内容的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"
  1. 创建 zip 格式的层:
zip -r -9 mylayer.zip python 
  1. 在AWS控制台中创建基于mylayer.zip的Lambda层。不要忘记指定Compatible runtimespython3.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']

1
非常感谢!工作非常顺利,就像涂了一层黄油一样 :) - user13888987

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接