title: AWS Lambda in Python: Import parent package/directory in Lambda function handler
body: <p>I have a directory structure like the following in my serverless application(simplest app to avoid clutter) which I created using AWS SAM with Python 3.8 as the runtime:</p>
  
  <pre><code>├── common
  │   └── a.py
  ├── hello_world
  │   ├── __init__.py
  │   ├── app.py
  │   └── requirements.txt
  └── template.yaml
  </code></pre>
  
  <p>I would like to import <code>common/a.py</code> module inside the Lambda handler - <code>hello_world/app.py</code>. Now I know that I can <a href="https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder">import it normally in Python</a> by adding the path to <code>PYTHONPATH</code> or <code>sys.path</code>, but it doesn't work when the code is run in Lambda inside a Docker container. When invoked, the Lambda handler function is run inside <code>/var/task</code> directory and the folder structure is not regarded. </p>
  
  <p>I tried inserting <code>/var/task/common</code>, <code>/var/common</code>, <code>/var</code> and even <code>/common</code> to <code>sys.path</code> programmatically like this:</p>
  
  <pre><code>import sys
  sys.path.insert(0, '/var/task/common')
  from common.a import Alpha
  </code></pre>
  
  <p>but I still get the error:</p>
  
  <p><code>ModuleNotFoundError: No module named 'common'</code></p>
  
  <p>I am aware of <strong><a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html" rel="noreferrer">Lambda Layers</a></strong> but given my scenario, I would like to directly reference the common code in multiple Lambda functions without the need of uploading to a layer. I want to have something like the <code>serverless-python-requirements</code> plugin in the <strong>Serverless framework</strong> but in AWS SAM.</p>
  
  <p>So my question is, how should I add this path to <code>common</code> to <code>PYTHONPATH</code> or <code>sys.path</code>? Or is there an alternative workaround for this like <code>[serverless-python-requirements][3]</code> to directly import a module in a parent folder without using Lambda Layers?</p>