title: AWS SAM Local and docker-lambda: keep getting Unable to import module 'lambda_function': No module named 'lambda_function'
body: <p><strong>Edit 2: The root cause was I had several DOCKER environment variables set which were causing my function invocations to re-route to a remote Docker host and not hit SAM Local. Once I unset those, the functions started running.</strong> </p>
  
  <p>Edit: I cloned docker-lambda and tried running one of their examples and get the same error.</p>
  
  <pre><code>docker run --rm -v "$PWD":/var/task lambci/lambda:python3.6
  START RequestId: 73a433fc-1d8a-4cdb-a66d-61bd667e13ba Version: $LATEST
  Unable to import module 'lambda_function': No module named 'lambda_function'
  END RequestId: 73a433fc-1d8a-4cdb-a66d-61bd667e13ba
  REPORT RequestId: 73a433fc-1d8a-4cdb-a66d-61bd667e13ba Duration: 1 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 15 MB
  
  {"errorMessage": "Unable to import module 'lambda_function'"}
  </code></pre>
  
  <p>I'm trying to set up SAM Local with a Python lambda function and keep getting frustrated by the module import error in the title.</p>
  
  <p>My template.yaml looks like this:</p>
  
  <pre><code>AWSTemplateFormatVersion : '2010-09-09'
  Transform: AWS::Serverless-2016-10-31
  Resources:
    ProposalsTable:
      Type: "AWS::Serverless::SimpleTable"
    AddProposal:
      Type: "AWS::Serverless::Function"
      Properties:
        Handler: lambda_function.lambda_handler
        Runtime: python3.6
        Policies: AmazonDynamoDBFullAccess
        Environment:
          Variables:
            TABLE_NAME: !Ref ProposalsTable
        Events:
          Vote:
            Type: Api
            Properties:
              Path: /proposals
              Method: get
  </code></pre>
  
  <p>I have a lambda_function.py in the same folder as the template.yaml.</p>
  
  <p>I run: <code>sam local start-api</code> and it starts up fine:</p>
  
  <pre><code>Mounting lambda_function.lambda_handler (python3.6) at http://127.0.0.1:3000/proposals [GET]
  </code></pre>
  
  <p>Then I do:</p>
  
  <pre><code>curl http://127.0.0.1:3000/proposals
  </code></pre>
  
  <p>Then on the "server" it shows:</p>
  
  <pre><code>Unable to import module 'lambda_function': No module named 'lambda_function'
  Function returned an invalid response (must include one of: body, headers or statusCode in the response object): %!s(&lt;nil&gt;)
  </code></pre>
  
  <p>I've tried all different ways of naming the file (e.g. putting it inside a folder with an <strong>init</strong>.py)</p>
  
  <p>I googled and read a dozen or more threads but most people are talking about deploying to the real AWS Lambda; there's not a lot on SAM Local.</p>
  
  <p>I wonder if it's something in my environment. The node.js sample function here fails with a timeout. <a href="https://github.com/awslabs/aws-sam-local/tree/develop/samples/hello-world/node" rel="nofollow noreferrer">https://github.com/awslabs/aws-sam-local/tree/develop/samples/hello-world/node</a></p>
  
  <pre><code>2018/01/04 15:20:41 Invoking index.handler (nodejs6.10)
  2018/01/04 15:20:41 Mounting /Users/me/code/sam-local-prototype as /var/task:ro inside runtime container
  2018/01/04 15:20:46 Function index.handler timed out after 3 seconds
  </code></pre>
  
  <p>Ideas?</p>