aws lambda Unable to import module 'lambda_function': No module named 'requests'

后端 未结 4 1013
北恋
北恋 2020-12-28 17:50

I have recently started to use AWS Lambda to use triggers against some python code I have written. I currently have 2 lambda functions, both of which have been created with

相关标签:
4条回答
  • 2020-12-28 18:18

    https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/

    AWS removed the vendored version of requests from Botocore.

    Steps:

    1. cmd >> pip install requests

    2. Python code:

      import requests
      response = requests.get('https://...') 
      
    0 讨论(0)
  • 2020-12-28 18:27

    So this is what resolved in my case

    Handler :: "yfinance_lamdba.lambda_handler"

    In the above line

                   **yfinance_lamdba** is filename 'yfinance_script.py'
    
                   **lambda_handler** is function 'def lambda_handler' which has actual code
    
    0 讨论(0)
  • 2020-12-28 18:41

    requests library doesn't come by default in lambda. It looks like you are trying to import it in your function / library somewhere. To import it, you need the following line:

    from botocore.vendored import requests
    

    Alternatively, you would need to zip the requests library in the root of your zip file.

    EDIT: There may be a dependency in one of your libraries that may need this. To overcome this, install requests in your application zip. To do this, run the following command in the root directory of your application: pip install requests -t ./.

    A better way would be to create a file called requirements.txt and add all the dependencies in there. Use virtualenv to install all the packages defined in the requirements.txt using: pip install -r requirements.txt -t ./


    UPDATE: Starting 10/21/19, the vendored version of the requests library in botocore will be removed. Refer this blog post for more details.

    0 讨论(0)
  • 2020-12-28 18:41

    Give it a check to this answer

    If you're working with Python on AWS Lambda, and need to use requests, you better use urllib3, it is currently supported on AWS Lambda and you can import it directly, check the example on urllib3 site.

    import urllib3
    
    http = urllib3.PoolManager()
    r = http.request('GET', 'http://httpbin.org/robots.txt')
    
    r.data
    # b'User-agent: *\nDisallow: /deny\n'
    r.status
    # 200
    
    0 讨论(0)
提交回复
热议问题