Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to correctly import modules in python on lambda project

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)

like image 340
Matthieu Veron Avatar asked Nov 06 '22 18:11

Matthieu Veron


1 Answers

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.

like image 77
sahasrara62 Avatar answered Nov 14 '22 07:11

sahasrara62