title: In Python Azure functions, how do I import a shared library in my function with just a single line?
body: <p>I'm using Python 3.8 and creating functions for Azure.  I have this directory structure ..</p>
  <pre><code>shared
      __init__.py
  my_function
      __init.py
  tests
      __init__.py
      functions
          __init__.py
          test_myfunction.py
  </code></pre>
  <p>In the shared/<strong>init</strong>.py file, I have some constants ...</p>
  <pre><code>import os
    
  HEADERS = {&quot;Content-Type&quot;: &quot;text/plain&quot;}
  </code></pre>
  <p>In my my_function/<strong>init</strong>.py file, I haven't figured out how to write a single line to include the shared file.  If I do this</p>
  <pre><code>from ..shared import HEADERS
  </code></pre>
  <p>I get this error when running pytest ..</p>
  <pre><code>pytest tests/functions/test_myfunction.py
  ...
  ERROR tests/functions/test_myfunction.py - ValueError: attempted relative import beyond top-level package
  </code></pre>
  <p>If I do this,</p>
  <pre><code>from shared import HEADERS
  </code></pre>
  <p>I get this error when starting my application (e.g. using &quot;func start&quot;) ...</p>
  <pre><code>Exception: ModuleNotFoundError: No module named 'shared'
  </code></pre>
  <p>I have to resort to using try-except</p>
  <pre><code>try:
      from ..shared import HEADERS
  except:
      from shared import HEADERS
  </code></pre>
  <p>but this just feels really messy and wrong.  What's the proper way to write a single line to include what I need for both?</p>
  <p><b>Edit:</b> Here are the main contents of the pytest test file, tests/functions/test_myfunction.py ...</p>
  <pre><code>_import = __import__('myfunction')
  ...
  req = func.HttpRequest(
          method='GET',
          body=None,
          url='/myfunction',
          params=params,
          headers=headers
      )
  resp = _import.main(req) 
  </code></pre>