簡體   English   中英

Runtime.ImportModuleError:無法導入模塊“lambda_function”:沒有名為“httplib2”的模塊

[英]Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named '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 的層。 但是,我不斷收到[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'httplib2'當我在本地運行我的 pyhon 文件時,我沒有收到任何錯誤調用import httplib2 注意:所有其他庫都適用於 lambda,所以我的上傳過程應該是正確的。

您可以使用您的包創建 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
  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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM