# Scenerio
You are trying to pack up your code and packages for lambda, you face this error, what might have happened.
```json
{
"errorMessage": "Unable to import module 'lambda_function': No module named 'lambda_function'",
"errorType": "Runtime.ImportModuleError",
"requestId": "",
"stackTrace": []
}
```
> [!Tip] To read more about packaging lambda:
> 補充教材 - [**利用 AI 工具把 Python 程式碼轉換成 Lambda 可以運行的格式,然後打包上傳 & Lambda 常見應用及可以解決客戶哪些痛點**](https://www.notion.so/AI-Python-Lambda-Lambda-14e6bfee6817804d906dfae67e3318d4?pvs=21)
# Breakdown
## Let’s take a look at the zip file
### Situation 1 : Select all the files and compress
```
**function.zip**
├── __pycache__
├── _mysql_connector.cpython-311-darwin.so
├── beautifulsoup4-4.12.2.dist-info
...
├── urllib3
├── urllib3-2.0.7.dist-info
└── **lambda_function.py**
```

### Situation 2 : Select the folder and compress
```
**function.zip**
└── **function**
├── __pycache__
├── _mysql_connector.cpython-311-darwin.so
├── beautifulsoup4-4.12.2.dist-info
...
├── urllib3
├── urllib3-2.0.7.dist-info
└── **lambda_function.py**
```

## So what’s the difference if you upload this to lambda.
### Situation 1 : Select all the files and compress
> [!Note]
> Your **lambda_function.py** and packages are in the parent folder which is named after the lambda.

### Situation 2 : Select the folder and compress
> [!Note]
> Your **lambda_function.py** and packages are in the function folder you have compressed.

## So what happens behind the error ?
The reason is that the settings of **Handler** is wrong.

**`lambda_function`**
- **檔案名稱**:`lambda_function.py` 是 Lambda 執行程式碼的檔案名稱,告訴 Lambda **程式碼在哪裡**。
- 例如,如果你的檔案是 `lambda_function.py`,Lambda 就會從這個檔案中尋找入口函式。
**`lambda_handler`**
- **入口函式名稱**:`lambda_handler` 是檔案中用來處理事件的函式名稱,Lambda 會從這個函式開始執行程式邏輯。
- 這是 AWS 預設的函式名稱,但你可以在 Lambda 設定中自訂。
> [!Important]
> **`lambda_function.lambda_handler`** is for situation 1 and **`function.lambda_function.lambda_handler`** is for situation 2
</aside>
### Add a folder name (`function`) in front of the handler.

## Let’s test Situation 2 again !
### This time we got this error
```
{
"errorMessage": "Unable to import module 'function.lambda_function': No module named 'requests'",
"errorType": "Runtime.ImportModuleError",
"requestId": "",
"stackTrace": []
}
```
> [!Note]
> ### We can’t locate where all my imported packages is !
> Situation 1 is the normal case and you won't get this error !
To fix this, simply add `sys.path` to your **lambda_function.py**
```python
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
```
- **What it does:** Adds the **current directory** (where the lambda_function.py file resides) to the sys.path.
- **When it works:** This works if the requests package is already located in the **same directory** as lambda_function.py or is structured in a way that Python can directly find it there.
- **Why it works:** When Python tries to resolve an import statement, **it checks each directory in sys.path in order.** By adding the current directory, you’re ensuring that Python can find any modules or packages in the same folder as lambda_function.py.

> [!Note]
> ### Everything works fine after that !!!