8

I am trying to import my own module but I am getting error:

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

lambda_function.py

enter image description here

Own modulename.py

enter image description here

5
  • 4
    Please don't post images, but text formatted as code inside the question Commented Sep 30, 2020 at 17:11
  • 1
    @Tomerikoo seriously!!! you need text for this question. Commented Sep 30, 2020 at 17:30
  • 2
    I don't know if that's supposed to be a question or a statement. And no, I don't need anything, this is not my question. You on the other hand, want to allow people to help you easily. They can't copy-paste and run your code if it's in an image... Commented Sep 30, 2020 at 17:32
  • @Tomerikoo, Sorry sir In future question I will keep in mind. Actually I wanted to show all details like file structure, error log. Commented Sep 30, 2020 at 18:06
  • 1
    No problem, that's good and important. It's just better to put it in the question in a more readable way. I'm sure you'll agree with me that it's hard to see anything from these pictures. Good luck Commented Oct 1, 2020 at 7:55

2 Answers 2

13

You import it as if you were to import any other Python module. In other words don't do this:

from .name import *

but do this:

from name import show_name

For example:

enter image description here

The contents of name.py:

def my_name():
    print("Your name goes here.")

Don't forget to Deploy your function after making changes.

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

1 Comment

Why some editors induce us to make this mistake? It should be a standard feature in pylint, for example!
6

An alternative approach that I find works well if your lambda function is a module is to save it as a module. So something like:

my_awesome_service
|--lambda_function.py
|--name.py
included_module_1
included_module_2

And then invoke using:

my_awesome_service.lambda_function.lambda_handler

Then the relative imports work and you don't need any changes to your code

1 Comment

This is a great approach, the unit tests in SAM CLI examples also work with this setup.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.