4

This is a simple SAM-template based deploy with a Docker container. Filesystem structure:

src/app.py
    mymodule.py

In app.py:

from .mymodule import myfunction

Result (on invoke):

Unable to import module 'app': attempted relative import with no known parent package

Removing the dot results in:

Unable to import module 'app': No module named 'mymodule'

Adding the local directory to path does not help either:

import os, sys
currentdir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(currentdir)

Now I guess this appears to be due to the limitations described in the great answer from Relative imports for the billionth time

i.e. app.py is being run as a script, not a module, and scripts cannot import relatively

The workarounds in the above answer both require changing some way the Lambda function is built and/or invoked - how to do this is the question?

2
  • 1
    Please try from mymodule import ... without dot before mymodule. Ref: stackoverflow.com/Questions/16981921/… Commented Oct 6, 2021 at 22:49
  • It then fails with "Unable to import module 'app': No module named 'mymodule'". Have also tried all the sys.path.append() ideas in that answer without any luck. Commented Oct 7, 2021 at 5:35

1 Answer 1

2

Add __init__.py file to your src/ folder (same level as your app.py)

Or if you are using a container, make sure your Dockerfile copies everything and not just app.py

Sign up to request clarification or add additional context in comments.

1 Comment

What can I say. It was the Dockerfile. Thanks for taking the time!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.