6

I'm making a lambda function in python. Here is the current structure of my project.

lambda/
|-- datas/
|   |-- d.json
|
|-- package_name/
|   |-- __init__.py
|   |-- lambda_function.py # this is using d.json
|   |-- a.py # this is some classes used on lambda_function
|   |-- b.py # this is some basic time functions that a.py need
|   |-- utils.py
|
|-- tests/
|   |-- __init__.py
|   |-- test_main.py
|-- setup.py
|-- README

I have some issues with imports.

# lambda_function.py files
from a import *
from utils import *

# a.py files
from b import *

# b.py files
from a import *

It's working locally, but not in aws lambda console. To make it working in aws lambda console, I need to change this :

# lambda_function.py files
from package_name.a import *

So my first question is : why ?

And my second question is : If I want to import package_name/a.py in tests/tests_main.py, what should I do ?

I tried

from a import *
from package_name import *

But it doesn't work

I'm still a little bit lost with how imports work, even after reading what internet had to say about it. Moreover, I'm not sure about my project files structure (but it's an other subject I guess)

2 Answers 2

1

use

# lambda_function.py files
from .a import *
from .utils import *

# a.py files
from .b import *

# b.py files
from .a import *

this will tell that from the current directory read the module and import all functions, classess, variables.

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

5 Comments

That's working on aws lambda console. But locally, I get this error : ModuleNotFoundError: No module named 'main.a'; 'main' is not a package. What's the difference between aws lambda console and when I execute lambda_function.py locally on vscode ?
@MatthieuVeron you can use something like python-lambda-local to test your function locally.
@MatthieuVeron is main your package name ?
@MatthieuVeron can you tell what is main , which you have wrote in code ?, better to add part where is error coming and what error it is
I don't have any file named "main". I don't really understand what do you want me to add. Here is the error : from .a import * ModuleNotFoundError: No module named 'main.a'; 'main' is not a package. The error appeared when I ran the code on lambda_function.py
0

You can try adding your current working directory path to the python libraries path

import sys
sys.path.append('../')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.