aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

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

Open FabricioPatrocinio opened this issue 2 years ago • 6 comments

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.

FabricioPatrocinio avatar Jan 22 '24 19:01 FabricioPatrocinio

Hmm, thanks for bringing up the idea. Ill bring up the feature to the team and add it to our list.

jysheng123 avatar Jan 24 '24 20:01 jysheng123

Hmm, thanks for bringing up the idea. Ill bring up the feature to the team and add it to our list.

If it's possible to contribute, I could submit a pull request, since it's written in Python

FabricioPatrocinio avatar Jan 24 '24 23:01 FabricioPatrocinio

Feel free to, we welcome any contributors to help improving our repository. Thanks!

jysheng123 avatar Jan 24 '24 23:01 jysheng123

But once here @jysheng123 , I really wanted to be able to make absolute imports, I haven't been able to make my contribution yet, and I wanted to know if it still makes sense, if so I want to be able to help do that, because how the project is organized, if I make absolute imports, my The main module does not work as a module in the build, but the modules are unpacked from it. So, can you tell me if you added something to resolve this, and if not, could you give me an idea of what to do to help make this possible?

FabricioPatrocinio avatar Apr 13 '24 19:04 FabricioPatrocinio