简体   繁体   English

如何在lambda项目中正确导入python中的模块

[英]how to correctly import modules in python on lambda project

I'm making a lambda function in python. 我在python中创建一个lambda函数。 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. 它在本地工作,但不在aws lambda控制台中。 To make it working in aws lambda console, I need to change this : 为了使它在aws lambda控制台中工作,我需要改变它:

# 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 ? 我的第二个问题是:如果我想在tests / tests_main.py中导入package_name / a.py,我该怎么办?

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) 而且,我不确定我的项目文件结构(但我认为这是另一个主题)

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. 这将告诉当前目录读取模块并导入所有函数,classess,变量。

You can try adding your current working directory path to the python libraries path 您可以尝试将当前工作目录路径添加到python库路径

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM