πŸ•·οΈ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 44 (from laksa163)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

πŸ“„
INDEXABLE
βœ…
CRAWLED
3 days ago
πŸ€–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.1 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://xebia.com/blog/python-and-relative-imports-in-aws-lambda-functions/
Last Crawled2026-04-11 06:42:58 (3 days ago)
First Indexed2023-06-06 01:29:00 (2 years ago)
HTTP Status Code200
Meta TitlePython And Relative Imports In AWS Lambda Functions | Xebia
Meta DescriptionWhen 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 Canonicalnull
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/) ![English](https://kcdn.xebia.com/cdn-cgi/image/scq=50,format=auto,quality=75,onerror=redirect,width=48/https://xebia.com/_next/static/media/usa_flag.371b2b09.png) 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](https://kcdn.xebia.com/cdn-cgi/image/scq=50,format=auto,quality=75,onerror=redirect,width=3840/https://xebia.com/media/2025/03/b71ac8c56e20db442a2da39c6a333f9a.png) #### Joris Conijn Updated January 27, 2026 3 minutes Share β€Œ ![](https://kcdn.xebia.com/cdn-cgi/image/scq=50,format=auto,quality=75,onerror=redirect,width=3840/https://xebia.com/media/2025/04/python-and-relative-imports-in-aws-lambda-functions-banner-1.jpg) 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. ![Example of the folder structure](https://xebia.com/media/2025/04/folder_structure.png) 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 ![](https://kcdn.xebia.com/cdn-cgi/image/scq=50,format=auto,quality=75,onerror=redirect,width=3840/https://xebia.com/media/2025/03/b71ac8c56e20db442a2da39c6a333f9a.png) #### 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/) [β€Œ ![Govern and Discover AI Agents and Tools with Amazon Bedrock AgentCore Registry](https://kcdn.xebia.com/cdn-cgi/image/scq=50,format=auto,quality=75,onerror=redirect,width=640/https://xebia.com/media/2026/04/govern-and-discover-ai-agents-and-tools-with-amazon-bedrock-agentcore-registry-banner.jpg)](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 [β€Œ ![Your Data Lakehouse Architecture Questions Answered: Open-Source Data Catalogs...](https://kcdn.xebia.com/cdn-cgi/image/scq=50,format=auto,quality=75,onerror=redirect,width=640/https://xebia.com/media/2026/04/Open-Source-Data-Catalogs-scaled.jpg)](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 [β€Œ ![Market Leaders vs Challengers: the Ongoing Battle for Data Catalogs in Data...](https://kcdn.xebia.com/cdn-cgi/image/scq=50,format=auto,quality=75,onerror=redirect,width=640/https://xebia.com/media/2026/04/Data-Catalog-Lakehouse-scaled.jpg)](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 [β€Œ ![Bridging the Business-Technology Gap with a Semantic Data Layer](https://kcdn.xebia.com/cdn-cgi/image/scq=50,format=auto,quality=75,onerror=redirect,width=640/https://xebia.com/media/2026/04/AI-Ready-Architecture_-Semantic-Layers_-Blog_Hero_Desktop_1440459_L3_L4-scaled.jpg)](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. ![Example of the folder structure](https://xebia.com/media/2025/04/folder_structure.png) 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 ![](https://kcdn.xebia.com/cdn-cgi/image/scq=50,format=auto,quality=75,onerror=redirect,width=3840/https://xebia.com/media/2025/03/b71ac8c56e20db442a2da39c6a333f9a.png) 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.
Shard44 (laksa)
Root Hash4774581871557049444
Unparsed URLcom,xebia!/blog/python-and-relative-imports-in-aws-lambda-functions/ s443