βΉοΈ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://xebia.com/blog/python-and-relative-imports-in-aws-lambda-functions/ |
| Last Crawled | 2026-04-11 06:42:58 (3 days ago) |
| First Indexed | 2023-06-06 01:29:00 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Python And Relative Imports In AWS Lambda Functions | Xebia |
| Meta Description | When writing an AWS Lambda function, it's quite possible that you get to the point where the file becomes too big. So, what do you do? You create a second |
| Meta Canonical | null |
| Boilerpipe Text | When writing an AWS Lambda function, it's quite possible that you get to the point where the file becomes too big. So, what do you do? You create a second file and refactor your code to do a relative import. Then, the only step left is to deploy your code and run it. And then it fails... Have you been here before? Then I might have some sound advice for you.
If you look at the
PEP 8 -- Style Guide for Python Code
.
You will read that
absolute imports are recommended. But, explicit relative imports are an acceptable alternative to absolute imports
.
This means that you should favor absolute imports. And only use relative imports within a package.
Sample code
Let's create a hello world lambda function to show this. So we have the following code in a file called
hello_world/app.py
:
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps({"message": "hello world"}),
}
For simplicity reasons I used the
AWS Serverless Application Model
.
The templates contain the following resource:
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
The most important thing is the
Handler
definition. The value
app.lambda_handler
translates to:
Use the
app.py
file and invoke the
lambda_handler
method.
We will now move the payload to a file called
hello_world/response.py
:
def get_response() -> dict:
return {"message": "hello world"}
And update the
hello_world/app.py
:
import json
from .response import get_response
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps(get_response()),
}
You run your test and it works, nice! So we will now deploy our code and that it also works. And it fails...
[ERROR] Runtime.ImportModuleError: Unable to import module 'app': attempted relative import with no known parent package
Traceback (most recent call last):
The content of the
hello_world
folder ends up in the
/var/task/
folder. And not the folder itself.
So it might look like that the code is in a package but from the Lambda runtime it's not.
The solution
You can solve this by creating a package. How you might ask? Create a
hello_world
folder in the
hello_world
folder.
Move all files except the
requirements.txt
into the created folder.
Next you need to update the
Handler
in the template:
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: hello_world.app.lambda_handler
Runtime: python3.9
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
Now the value of the
Handler
translates to:
In the
hello_world
package you use the
app.py
file and invoke the
lambda_handler
method.
When you deploy you will now notice that the relative import works.
Conclusion
I would always tell you to deploy your python code in a package. It makes it easier to use many files and organize your code better.
It also allows you to separate the Lambda invocation logic from your business logic. Making it easier to test and maintain.
Image by Gerd Altmann from Pixabay
Written by
Joris is the AWS Practise CTO of the Xebia Cloud service line and has been working with the AWS cloud since 2009 and focussing on building event-driven architectures. While working with the cloud from (almost) the start, he has seen most of the services being launched.
Joris strongly believes in automation and infrastructure as code and is open to learning new things and experimenting with them because that is the way to learn and grow. |
| Markdown | - [Artificial Intelligence](https://xebia.com/artificial-intelligence/)
- [Intelligent Automation](https://xebia.com/intelligent-automation/)
- [Cloud & Data](https://xebia.com/cloud-data-modernization/)
- [Products & Platforms](https://xebia.com/digital-products-platforms/)
- [Industries](https://xebia.com/industries/)
- [Partners](https://xebia.com/partners/)
- [Solutions](https://xebia.com/solutions/)
- [Training](https://academy.xebia.com/)
- [Careers](https://xebia.com/careers/)
- [About](https://xebia.com/blog/python-and-relative-imports-in-aws-lambda-functions/)
[Contact](https://xebia.com/about-us/contact/)

AiOverview
This AI search assistant is currently in a pilot program and is still being refined. Responses, generated in English, may take a few seconds to appear. We aim for accuracy, but occasional inaccuracies may occur.
Please verify key details before making decisions or [contacting us](https://xebia.com/about-us/contact/) directly.
##### Response
β
β
β
##### Related Topics
β
β
β
β
1. β
β
β
β
β
β
2. β
β
β
β
β
β
3. β
β
β
β
β
β
4. β
β
β
β
β
β
##### Related Topics
β
β
β
β
- [Home](https://xebia.com/)
- [Blog](https://xebia.com/blog/)
- Python and relative imports in AWS Lambda Functions
Blog
# Python and relative imports in AWS Lambda Functions

#### Joris Conijn
Updated January 27, 2026
3 minutes
Share
β

When writing an AWS Lambda function, it's quite possible that you get to the point where the file becomes too big. So, what do you do? You create a second file and refactor your code to do a relative import. Then, the only step left is to deploy your code and run it. And then it fails... Have you been here before? Then I might have some sound advice for you.
If you look at the [PEP 8 -- Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/#imports). You will read that *absolute imports are recommended. But, explicit relative imports are an acceptable alternative to absolute imports*. This means that you should favor absolute imports. And only use relative imports within a package.
## Sample code
Let's create a hello world lambda function to show this. So we have the following code in a file called `hello_world/app.py`:
```
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps({"message": "hello world"}),
}
```
For simplicity reasons I used the [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html). The templates contain the following resource:
```
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
```
The most important thing is the `Handler` definition. The value `app.lambda_handler` translates to:
> Use the `app.py` file and invoke the `lambda_handler` method.
We will now move the payload to a file called `hello_world/response.py`:
```
def get_response() -> dict:
return {"message": "hello world"}
```
And update the `hello_world/app.py`:
```
import json
from .response import get_response
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps(get_response()),
}
```
You run your test and it works, nice! So we will now deploy our code and that it also works. And it fails...
```
[ERROR] Runtime.ImportModuleError: Unable to import module 'app': attempted relative import with no known parent package
Traceback (most recent call last):
```
The content of the `hello_world` folder ends up in the `/var/task/` folder. And not the folder itself. So it might look like that the code is in a package but from the Lambda runtime it's not.
## The solution
You can solve this by creating a package. How you might ask? Create a `hello_world` folder in the `hello_world` folder. Move all files except the `requirements.txt` into the created folder.

Next you need to update the `Handler` in the template:
```
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: hello_world.app.lambda_handler
Runtime: python3.9
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
```
Now the value of the `Handler` translates to:
> In the `hello_world` package you use the `app.py` file and invoke the `lambda_handler` method.
When you deploy you will now notice that the relative import works.
## Conclusion
I would always tell you to deploy your python code in a package. It makes it easier to use many files and organize your code better. It also allows you to separate the Lambda invocation logic from your business logic. Making it easier to test and maintain.
Image by Gerd Altmann from Pixabay
***
Tags:
- [AWSβ](https://xebia.com/partner/aws/)
- [Cloud](https://xebia.com/capability/cloud/)
- [Data & Analytics](https://xebia.com/capability/data-analytics/)
### Written by

#### Joris Conijn
Joris is the AWS Practise CTO of the Xebia Cloud service line and has been working with the AWS cloud since 2009 and focussing on building event-driven architectures. While working with the cloud from (almost) the start, he has seen most of the services being launched. Joris strongly believes in automation and infrastructure as code and is open to learning new things and experimenting with them because that is the way to learn and grow.
Our Ideas
## Explore More Blogs
[View All](https://xebia.com/blog/)
[β ](https://xebia.com/blog/govern-and-discover-ai-agents-and-tools-with-amazon-bedrock-agentcore-registry/)
[Govern and Discover AI Agents and Tools with Amazon Bedrock AgentCore Registry](https://xebia.com/blog/govern-and-discover-ai-agents-and-tools-with-amazon-bedrock-agentcore-registry/)
Joris Conijn
[β ](https://xebia.com/blog/data-lakehouse-architecture-open-source-data-catalogs-vs-market-leaders/)
[Your Data Lakehouse Architecture Questions Answered: Open-Source Data Catalogs...](https://xebia.com/blog/data-lakehouse-architecture-open-source-data-catalogs-vs-market-leaders/)
Marek WiewiΓ³rka
[β ](https://xebia.com/blog/market-leaders-challengers-data-catalogs-data-lakehouse/)
[Market Leaders vs Challengers: the Ongoing Battle for Data Catalogs in Data...](https://xebia.com/blog/market-leaders-challengers-data-catalogs-data-lakehouse/)
Marek WiewiΓ³rka
[β ](https://xebia.com/blog/bridging-business-tech-gap-semantic-data-layer/)
[Bridging the Business-Technology Gap with a Semantic Data Layer](https://xebia.com/blog/bridging-business-tech-gap-semantic-data-layer/)
Marcel Ploska
Contact
## Letβs discuss how we can support your journey.
β
β
β
β
β
β
β
β
β
- [Industries](https://xebia.com/industries/)
- [Solutions](https://xebia.com/solutions/)
- [Partner Ecosystem](https://xebia.com/partners/)
- [Our Work](https://xebia.com/customer-stories/)
- [Our Ideas](https://xebia.com/our-ideas/)
- [Legal](https://xebia.com/legal/)
- [Privacy Statement](https://xebia.com/privacy-statement/)
- [Cookie Statement](https://xebia.com/cookie-statement/)
- [Accessibility Statement](https://xebia.com/accessibility-statement/)
- [Accreditations](https://xebia.com/accreditations/)
- [Sitemap](https://xebia.com/sitemap/)
- [About Us](https://xebia.com/about-us/)
- [Leadership](https://xebia.com/team/)
- [Contact Us](https://xebia.com/about-us/contact/)
- [Life at Xebia](https://xebia.com/life-at-xebia/)
- [Newsletters](https://xebia.com/newsletters/)
- [We are hiring\!](https://xebia.com/careers/)
Β© 2025 Xebia. All Rights Reserved.
Connect with Us: |
| Readable Markdown | When writing an AWS Lambda function, it's quite possible that you get to the point where the file becomes too big. So, what do you do? You create a second file and refactor your code to do a relative import. Then, the only step left is to deploy your code and run it. And then it fails... Have you been here before? Then I might have some sound advice for you.
If you look at the [PEP 8 -- Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/#imports). You will read that *absolute imports are recommended. But, explicit relative imports are an acceptable alternative to absolute imports*. This means that you should favor absolute imports. And only use relative imports within a package.
## Sample code
Let's create a hello world lambda function to show this. So we have the following code in a file called `hello_world/app.py`:
```
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps({"message": "hello world"}),
}
```
For simplicity reasons I used the [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html). The templates contain the following resource:
```
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
```
The most important thing is the `Handler` definition. The value `app.lambda_handler` translates to:
> Use the `app.py` file and invoke the `lambda_handler` method.
We will now move the payload to a file called `hello_world/response.py`:
```
def get_response() -> dict:
return {"message": "hello world"}
```
And update the `hello_world/app.py`:
```
import json
from .response import get_response
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps(get_response()),
}
```
You run your test and it works, nice! So we will now deploy our code and that it also works. And it fails...
```
[ERROR] Runtime.ImportModuleError: Unable to import module 'app': attempted relative import with no known parent package
Traceback (most recent call last):
```
The content of the `hello_world` folder ends up in the `/var/task/` folder. And not the folder itself. So it might look like that the code is in a package but from the Lambda runtime it's not.
## The solution
You can solve this by creating a package. How you might ask? Create a `hello_world` folder in the `hello_world` folder. Move all files except the `requirements.txt` into the created folder.

Next you need to update the `Handler` in the template:
```
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: hello_world.app.lambda_handler
Runtime: python3.9
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
```
Now the value of the `Handler` translates to:
> In the `hello_world` package you use the `app.py` file and invoke the `lambda_handler` method.
When you deploy you will now notice that the relative import works.
## Conclusion
I would always tell you to deploy your python code in a package. It makes it easier to use many files and organize your code better. It also allows you to separate the Lambda invocation logic from your business logic. Making it easier to test and maintain.
Image by Gerd Altmann from Pixabay
### Written by

Joris is the AWS Practise CTO of the Xebia Cloud service line and has been working with the AWS cloud since 2009 and focussing on building event-driven architectures. While working with the cloud from (almost) the start, he has seen most of the services being launched. Joris strongly believes in automation and infrastructure as code and is open to learning new things and experimenting with them because that is the way to learn and grow. |
| Shard | 44 (laksa) |
| Root Hash | 4774581871557049444 |
| Unparsed URL | com,xebia!/blog/python-and-relative-imports-in-aws-lambda-functions/ s443 |