Skip to content

Adjust the sam build to use absolute imports for python projects by default and load the module from the project root in the build. #6593

@FabricioPatrocinio

Description

@FabricioPatrocinio

Use absolute imports for python projects by default and load the module from the project root in the build.

The build should consider the wrapping of the module that gives rise to the functionality, because the way it is done today we must use relative imports, since during the build only what is inside my root module is loaded, so far so good, relative imports solve it, but the problem is in the tests, they are in the same hierarchy as my root module and must first of all import this module reference.

The problem is that if I rotate the test imports using absolute form, the build imports will fail and so on.

Below I will better describe my idea using a recent project I am working on.

Organization of my folders according to sam init:

My project, I used sam-cli to generate a project using aws-power-tools in python

Normally in python projects we use absolute imports, in the case of my example it would be appropriate if I could use from app.utils.logger import logger
, however in the way deploy is defined the app module is only used as a file directory and not a module.

├── .aws-sam                  # Once the build is done, my modules look like this
│   ├── build
│   |  ├── app
│   |  |  ├── main.py         # To work I must import: from utils.logger import logger
|   |  |  ├── utils/logger.py
├── app                       # My modules
|   ├── main.py
|   ├── utils/logger.py
├── tests                     # My tests are breaking because they don't find the utils module in the app/main.py file
|   └── unit/test_handler.py  # This gives an import error: from app.main.py import handler

tests/unit/test_handler.py -> In this example, pytest will not find the utils module that is being imported in app/main.py

Directory generated by the build on s3

It works using import in a relative way, even if I use it this way I will still have problems with the tests because when entering the app module it will cause confusion because /tests is in the same hierarchy as the app module

├── db6d9afe326f91400fc6d106d421cfa6
|   ├── aws_power_tools
|   ├── other_libs, ex: boto3, botocore
│   ├── main.py
|   ├── utils/logger.py

Proposal

Adjust the sam build to use absolute imports by default and load the module from the project root in the build.
I suggest that it is possible to build the app module, for example, to actually treat it as a module and not just a working directory:

├── db6d9afe326f91400fc6d106d421cfa6
|   ├── aws_power_tools
|   ├── other_libs, ex: boto3, botocore
│   ├── app
│   │   ├──main.py
|   │   ├── utils/logger.py

With this I can make imports absolutely without having problems in the build and tests, for example: from app.module import function

Additional Details

If my idea is confused, I can create a project on my github to be used as an example, demonstrating the problem in question.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions