title: Python not finding module when running AWS sam local invoke
body: <p>I have just started using AWS serverless using SAM and have run into the problem below:</p>
  
  <p>SAM invoke can't seem to find a python module that my lambda handler is importing but I can otherwise import that module. </p>
  
  <p>The <strong>structure</strong> of my project is:</p>
  
  <p>root-dir/</p>
  
  <ul>
  <li>mymodulename/<br>
  
  <ul>
  <li>mymodule.py<br></li>
  </ul></li>
  <li>aws/<br>
  
  <ul>
  <li>sam/<br>
  
  <ul>
  <li>template.yaml</li>
  <li>packaged.yaml</li>
  <li>myawsservice/<br>
  
  <ul>
  <li>app.py<br></li>
  </ul></li>
  <li>test/<br>
  
  <ul>
  <li>test_event.json</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>I am working in a python3.6 virtual environment and I have installed my python package (mymodulename) using setup tool:</p>
  
  <pre><code>python setup.py develop
  </code></pre>
  
  <p>So if I do a pip freeze in my virtual environment I see:</p>
  
  <pre><code>Pillow==6.0.0
  PyPDF2==1.26.0
  reportlab==3.5.20
  mymodulename==0.5.0
  </code></pre>
  
  <p>Also if I go:</p>
  
  <pre><code>cd root-dir/aws/sam/
  python
  
  &gt;&gt;&gt; import mymodulename
  </code></pre>
  
  <p>This succeeds. So it is on the python path.</p>
  
  <p>But when I invoke sam locally i.e.:</p>
  
  <pre><code>sam local invoke MyAWSServiceFunction --event ../sam/test/test_event.json
  </code></pre>
  
  <p>I get the <strong>error</strong> message:</p>
  
  <pre><code>2019-05-02 09:19:17 Found credentials in shared credentials file: ~/.aws/credentials
  2019-05-02 09:19:18 Invoking app.lambda_handler (python3.6)
  
  Fetching lambci/lambda:python3.6 Docker container image......
  2019-05-02 09:19:19 Mounting /home/myname/root-dir/aws/sam/myawsservice as /var/task:ro,delegated inside runtime container
  START RequestId: 245daefe-ecfb-4530-9d15-cf07f55e0f3d Version: $LATEST
  Unable to import module 'app': No module named 'mymodulename'
  END RequestId: 245daefe-ecfb-4530-9d15-cf07f55e0f3d
  REPORT RequestId: 245daefe-ecfb-4530-9d15-cf07f55e0f3d Duration: 31 ms 
  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB
  
  {"errorMessage": "Unable to import module 'app'"}
  </code></pre>
  
  <p>Here is the code for my <strong>app.py</strong>:</p>
  
  <pre><code>from mymodulename.mymodule import mymodulefunction
  
  def lambda_handler(event, context):    
      return mymodulefunction(event['body'])
  </code></pre>
  
  <p>I can run the code above from my unit test just not from the sam invoke environment. </p>
  
  <p><strong>template.yaml:</strong></p>
  
  <pre><code>AWSTemplateFormatVersion: '2010-09-09'
  Transform: AWS::Serverless-2016-10-31
  Description: &gt; something
  
  Globals:
    Function:
      Timeout: 3
  
  Resources:
    MyAWSServiceFunction:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: myawsservice/
        Handler: app.lambda_handler
        Runtime: python3.6
  </code></pre>
  
  <p>Help appreciated. </p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>One workaround is to put the app.py, requirements.txt and template.yaml in the root directory and do:</p>
  
  <pre><code>sam build
  </code></pre>
  
  <p>This will store the 3rd party dependencies and my own python module together and template.py will now be able to find it during sam local invoke. </p>
  
  <p>This is however not practical for bigger projects, and I guess the answer for now might be something like <a href="https://medium.com/@nate_mitchell/serverless-packaging-user-defined-python-modules-41808776eae0" rel="noreferrer">this</a>. Although this is for a serverless.yaml. I'm not sure if SAM supports this syntax. Guessing no. </p>