Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello World, Unable to import module 'app': No module named 'app' #1345

Closed
sidsamant opened this issue Aug 17, 2019 · 8 comments
Closed

Hello World, Unable to import module 'app': No module named 'app' #1345

sidsamant opened this issue Aug 17, 2019 · 8 comments

Comments

@sidsamant
Copy link

Description

Trying to follow the tutorial at https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html#serverless-getting-started-hello-world-troubleshooting

Steps to reproduce

sam init --runtime python3.6

cd sam-app
sam build
sam local start-api

Observed result

In chrome, opening http://127.0.0.1:3000/hello
no data

In console,

Fetching lambci/lambda:python3.6 Docker container image......
2019-08-18 04:22:31 Mounting C:\Users\XXXX\sam-app\.aws-sam\build\HelloWorldFunction as /var/task:ro inside runtime container
START RequestId: 3ba1f0e5-fd1a-448d-86f2-c69f2927dbfe Version: $LATEST
Unable to import module 'app': No module named 'app'
END RequestId: 3ba1f0e5-fd1a-448d-86f2-c69f2927dbfe
REPORT RequestId: 3ba1f0e5-fd1a-448d-86f2-c69f2927dbfe Duration: 1 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB
2019-08-18 04:22:32 No Content-Type given. Defaulting to 'application/json'.
2019-08-18 04:22:32 127.0.0.1 - - [18/Aug/2019 04:22:32] "GET /hello HTTP/1.1" 200 -
2019-08-18 04:22:32 127.0.0.1 - - [18/Aug/2019 04:22:32] "GET /favicon.ico HTTP/1.1" 403 

Please provide command output with --debug flag set.

2019-08-18 03:52:40 Using SAM Template at C:\Users\XXXX\sam-app\.aws-sam\build\template.yaml
2019-08-18 03:52:40 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-08-18 03:52:40 Changing event name from before-call.apigateway to before-call.api-gateway
2019-08-18 03:52:40 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-08-18 03:52:40 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-08-18 03:52:40 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-08-18 03:52:40 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-08-18 03:52:40 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-08-18 03:52:40 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-08-18 03:52:40 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-08-18 03:52:40 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-08-18 03:52:40 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-08-18 03:52:40 Changing event name from creating-client-class.iot-data to creating-client-class.iot-data-plane
2019-08-18 03:52:40 Changing event name from before-call.apigateway to before-call.api-gateway
2019-08-18 03:52:40 Changing event name from request-created.machinelearning.Predict to request-created.machine-learning.Predict
2019-08-18 03:52:40 Changing event name from before-parameter-build.autoscaling.CreateLaunchConfiguration to before-parameter-build.auto-scaling.CreateLaunchConfiguration
2019-08-18 03:52:40 Changing event name from before-parameter-build.route53 to before-parameter-build.route-53
2019-08-18 03:52:40 Changing event name from request-created.cloudsearchdomain.Search to request-created.cloudsearch-domain.Search
2019-08-18 03:52:40 Changing event name from docs.*.autoscaling.CreateLaunchConfiguration.complete-section to docs.*.auto-scaling.CreateLaunchConfiguration.complete-section
2019-08-18 03:52:40 Changing event name from before-parameter-build.logs.CreateExportTask to before-parameter-build.cloudwatch-logs.CreateExportTask
2019-08-18 03:52:40 Changing event name from docs.*.logs.CreateExportTask.complete-section to docs.*.cloudwatch-logs.CreateExportTask.complete-section
2019-08-18 03:52:40 Changing event name from before-parameter-build.cloudsearchdomain.Search to before-parameter-build.cloudsearch-domain.Search
2019-08-18 03:52:40 Changing event name from docs.*.cloudsearchdomain.Search.complete-section to docs.*.cloudsearch-domain.Search.complete-section
2019-08-18 03:52:40 local start-api command is called
2019-08-18 03:52:40 Looking for credentials via: env
2019-08-18 03:52:40 Looking for credentials via: assume-role
2019-08-18 03:52:40 Looking for credentials via: shared-credentials-file
2019-08-18 03:52:40 Found credentials in shared credentials file: ~/.aws/credentials
2019-08-18 03:52:40 Loading JSON file: D:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\botocore\data\endpoints.json
2019-08-18 03:52:40 Event choose-service-name: calling handler <function handle_service_name_alias at 0x000001706550F620>
2019-08-18 03:52:40 Loading JSON file: D:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\botocore\data\serverlessrepo\2017-09-08\service-2.json
2019-08-18 03:52:40 Event creating-client-class.serverlessapplicationrepository: calling handler <function add_generate_presigned_url at 0x00000170654CD8C8>
2019-08-18 03:52:40 The s3 config key is not a dictionary type, ignoring its value of: None
2019-08-18 03:52:40 Setting serverlessrepo timeout as (60, 60)
2019-08-18 03:52:40 Loading JSON file: D:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\botocore\data\_retry.json
2019-08-18 03:52:40 Registering retry handlers for service: serverlessrepo
2019-08-18 03:52:40 No Parameters detected in the template
2019-08-18 03:52:40 2 resources found in the template
2019-08-18 03:52:40 Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2019-08-18 03:52:40 Trying paths: ['C:\\Users\\XXXX\\.docker\\config.json', 'C:\\Users\\XXXX\\.dockercfg']
2019-08-18 03:52:40 No config file found
2019-08-18 03:52:40 Trying paths: ['C:\\Users\\XXXX\\.docker\\config.json', 'C:\\Users\\XXXX\\.dockercfg']
2019-08-18 03:52:40 No config file found
2019-08-18 03:52:40 Starting new HTTPS connection (1): 192.168.99.100:2376
2019-08-18 03:52:40 https://192.168.99.100:2376 "GET /v1.35/_ping HTTP/1.1" 200 2
2019-08-18 03:52:40 Event choose-service-name: calling handler <function handle_service_name_alias at 0x000001706550F620>
2019-08-18 03:52:40 Event creating-client-class.serverlessapplicationrepository: calling handler <function add_generate_presigned_url at 0x00000170654CD8C8>
2019-08-18 03:52:40 The s3 config key is not a dictionary type, ignoring its value of: None
2019-08-18 03:52:40 Setting serverlessrepo timeout as (60, 60)
2019-08-18 03:52:40 Registering retry handlers for service: serverlessrepo
2019-08-18 03:52:40 No Parameters detected in the template
2019-08-18 03:52:40 2 resources found in the template
2019-08-18 03:52:40 Found '1' API Events in Serverless function with name 'HelloWorldFunction'
2019-08-18 03:52:40 Detected Inline Swagger definition
2019-08-18 03:52:40 Lambda function integration not found in Swagger document at path='/hello' method='get'
2019-08-18 03:52:40 Found '0' APIs in resource 'ServerlessRestApi'
2019-08-18 03:52:40 Removed duplicates from '0' Explicit APIs and '1' Implicit APIs to produce '1' APIs
2019-08-18 03:52:40 1 APIs found in the template
2019-08-18 03:52:40 Event choose-service-name: calling handler <function handle_service_name_alias at 0x000001706550F620>
2019-08-18 03:52:40 Loading JSON file: D:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\botocore\data\lambda\2015-03-31\service-2.json
2019-08-18 03:52:40 Event creating-client-class.lambda: calling handler <function add_generate_presigned_url at 0x00000170654CD8C8>
2019-08-18 03:52:40 The s3 config key is not a dictionary type, ignoring its value of: None
2019-08-18 03:52:40 Setting lambda timeout as (60, 60)
2019-08-18 03:52:40 Registering retry handlers for service: lambda
2019-08-18 03:52:40 Trying paths: ['C:\\Users\\XXXX\\.docker\\config.json', 'C:\\Users\\XXXX\\.dockercfg']
2019-08-18 03:52:40 No config file found
2019-08-18 03:52:40 Trying paths: ['C:\\Users\\XXXX\\.docker\\config.json', 'C:\\Users\\XXXX\\.dockercfg']
2019-08-18 03:52:41 No config file found
2019-08-18 03:52:41 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2019-08-18 03:52:41 You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2019-08-18 03:52:41 Localhost server is starting up. Multi-threading = True
2019-08-18 03:52:41  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2019-08-18 03:54:44 Constructed String representation of Event to invoke Lambda. Event: {"httpMethod": "GET", "body": null, "resource": "/hello", "requestContext": {"resourceId": "123456", "apiId": "1234567890", "resourcePath": "/hello", "httpMethod": "GET", "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", "accountId": "123456789012", "stage": "prod", "identity": {"apiKey": null, "userArn": null, "cognitoAuthenticationType": null, "caller": null, "userAgent": "Custom User Agent String", "user": null, "cognitoIdentityPoolId": null, "cognitoAuthenticationProvider": null, "sourceIp": "127.0.0.1", "accountId": null}, "extendedRequestId": null, "path": "/hello"}, "queryStringParameters": null, "headers": {"Host": "127.0.0.1:3000", "Connection": "keep-alive", "Cache-Control": "max-age=0", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-User": "?1", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", "Sec-Fetch-Site": "none", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-US,en;q=0.9", "X-Forwarded-Proto": "http", "X-Forwarded-Port": "3000"}, "pathParameters": null, "stageVariables": null, "path": "/hello", "isBase64Encoded": false}
2019-08-18 03:55:06 Found one Lambda function with name 'HelloWorldFunction'
2019-08-18 03:55:06 Invoking app.lambda_handler (python3.6)
2019-08-18 03:55:06 Environment variables overrides data is standard format
2019-08-18 03:55:06 Loading AWS credentials from session with profile 'default'
2019-08-18 03:55:06 Resolving code path. Cwd=C:\Users\XXXX\sam-app\.aws-sam\build, CodeUri=HelloWorldFunction
2019-08-18 03:55:06 Resolved absolute path to code is C:\Users\XXXX\sam-app\.aws-sam\build\HelloWorldFunction
2019-08-18 03:55:06 Code C:\Users\XXXX\sam-app\.aws-sam\build\HelloWorldFunction is not a zip/jar file
2019-08-18 03:55:06 Skipping building an image since no layers were defined
2019-08-18 03:55:06 Trying paths: ['C:\\Users\\XXXX\\.docker\\config.json', 'C:\\Users\\XXXX\\.dockercfg']
2019-08-18 03:55:06 No config file found
2019-08-18 03:55:06 Trying paths: ['C:\\Users\\XXXX\\.docker\\config.json', 'C:\\Users\\XXXX\\.dockercfg']
2019-08-18 03:55:06 No config file found
2019-08-18 03:55:06 https://192.168.99.100:2376 "GET /v1.35/images/lambci/lambda:python3.6/json HTTP/1.1" 200 None
2019-08-18 03:55:06 Looking for auth config
2019-08-18 03:55:06 No auth config in memory - loading from filesystem
2019-08-18 03:55:06 Trying paths: ['C:\\Users\\XXXX\\.docker\\config.json', 'C:\\Users\\XXXX\\.dockercfg']
2019-08-18 03:55:06 No config file found
2019-08-18 03:55:06 Looking for auth entry for 'docker.io'
2019-08-18 03:55:06 No entry found
2019-08-18 03:55:06 No auth config found
2019-08-18 03:55:10 https://192.168.99.100:2376 "POST /v1.35/images/create?tag=python3.6&fromImage=lambci%2Flambda HTTP/1.1" 200 None

Fetching lambci/lambda:python3.6 Docker container image......
2019-08-18 03:55:10 Mounting C:\Users\XXXX\sam-app\.aws-sam\build\HelloWorldFunction as /var/task:ro inside runtime container
2019-08-18 03:55:10 Starting new HTTPS connection (1): 192.168.99.100:2376
2019-08-18 03:55:10 https://192.168.99.100:2376 "POST /v1.35/containers/create HTTP/1.1" 201 88
2019-08-18 03:55:10 https://192.168.99.100:2376 "GET /v1.35/containers/a157d80f0d79e848331551551f431b7efb5f70f0abcdd38c872ad0acc32e5c4d/json HTTP/1.1" 200 None
2019-08-18 03:55:10 https://192.168.99.100:2376 "GET /v1.35/containers/a157d80f0d79e848331551551f431b7efb5f70f0abcdd38c872ad0acc32e5c4d/json HTTP/1.1" 200 None
2019-08-18 03:55:11 https://192.168.99.100:2376 "POST /v1.35/containers/a157d80f0d79e848331551551f431b7efb5f70f0abcdd38c872ad0acc32e5c4d/start HTTP/1.1" 204 0
2019-08-18 03:55:11 Starting a timer for 3 seconds for function 'HelloWorldFunction'
2019-08-18 03:55:11 https://192.168.99.100:2376 "GET /v1.35/containers/a157d80f0d79e848331551551f431b7efb5f70f0abcdd38c872ad0acc32e5c4d/json HTTP/1.1" 200 None
2019-08-18 03:55:11 https://192.168.99.100:2376 "POST /containers/a157d80f0d79e848331551551f431b7efb5f70f0abcdd38c872ad0acc32e5c4d/attach?stdout=1&stderr=1&logs=1&stream=1&stdin=0 HTTP/1.1" 101 0
START RequestId: 806dd433-da87-4c33-a5d9-9953372d5a30 Version: $LATEST
Unable to import module 'app': No module named 'app'
END RequestId: 806dd433-da87-4c33-a5d9-9953372d5a30
REPORT RequestId: 806dd433-da87-4c33-a5d9-9953372d5a30 Duration: 1 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB
2019-08-18 03:55:11 Starting new HTTPS connection (2): 192.168.99.100:2376
2019-08-18 03:55:11 https://192.168.99.100:2376 "GET /v1.35/containers/a157d80f0d79e848331551551f431b7efb5f70f0abcdd38c872ad0acc32e5c4d/json HTTP/1.1" 200 None
2019-08-18 03:55:11 https://192.168.99.100:2376 "DELETE /v1.35/containers/a157d80f0d79e848331551551f431b7efb5f70f0abcdd38c872ad0acc32e5c4d?v=False&link=False&force=True HTTP/1.1" 204 0
2019-08-18 03:55:11 No Content-Type given. Defaulting to 'application/json'.
2019-08-18 03:55:11 127.0.0.1 - - [18/Aug/2019 03:55:11] "GET /hello HTTP/1.1" 200 -
2019-08-18 03:55:11 127.0.0.1 - - [18/Aug/2019 03:55:11] "GET /favicon.ico HTTP/1.1" 403 -

Expected result

In console, as per tutorial

 Fetching lambci/lambda:python3.6 Docker container image......
 2019-07-12 15:29:58 Mounting /<working-development-path>/sam-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
 START RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST
 END RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72
 REPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72  Duration: 7.92 ms       Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 22 MB
 {"statusCode":200,"body":"{\"message\": \"hello world\"}"}

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Windows 10
  2. sam --version: SAM CLI, version 0.10.0

Add --debug flag to command you are running

@sanathkr
Copy link
Contributor

Have you shared the C:\ drive with Docker? This usually happens because the container mount failed.

@sidsamant
Copy link
Author

sidsamant commented Aug 25, 2019

I did share the drives but the mounts failed. Its a docker - VM issue as there are mount errors. Thanks for the tip.

@jpbelang
Copy link

I'm was having pretty much the same problem on MacOS using docker-toolbox.

Docker version 19.03.1, build 74b1e89
SAM CLI, version 0.21.0

The directories seemed to mount because they appeared in my virtualbox. However, the contents of the directory were empty.

I logged into my vm and hand mounted my home directory:

sudo mount -t vboxsf -o ro /Users /Users

And everything seemed to start working.

@sriram-mv
Copy link
Contributor

Closing this issue as it looks be docker related mounting issues.

@srisaithilak
Copy link

I know this is old thread, but since I could not find much help online or understand what is explained in above thread, putting my two cents.
You have to create your project folder in C:\Users<username>...... so that docker toolbox has access to it. Else it will fail. Otherwise, you can follow the tutorial on http://support.divio.com/en/articles/646695-how-to-use-a-directory-outside-c-users-with-docker-toolbox-docker-for-windows to mount the project folder on the docker container.
Hope it helps.

@dominusmi
Copy link

To add to this, same thing on linux - if the code is in, for example, /tmp, it won't work. Needs to be in /home/{user}

@crwgregory
Copy link

I was having this same issue on macos.
I was able to get around it by adding the --not-cached flag to the aws build command.
sam build --no-cached

My use case is that I'm listening to changes to python and yaml files and rebuild the app on changes to have a "live reload" type of functionality. My make file is as such:

run:
	@sam build --no-cached
	sam local start-api --force-image-build

run-debug:
	@sam build --no-cached
	sam local start-api --debug

watch-build: # npm i -g nodemon    https://github.com/aws/aws-sam-cli/issues/921#issuecomment-613710539
	@nodemon -d 1 -e "py yaml" --exec sam build --no-cached

@OSIVZ
Copy link

OSIVZ commented Sep 15, 2023

Dear folks,

I read through this thread. There is a reference to the fact that on Linux (WSL falls into the detention) , a code must be in /home/{user} and in my case it is, yet the issue persists.
Running environment:
Windows 11, WSL 1
SAM CLI, version 1.97.0
Docker desktop version 4.21.1 (114176

The code is located in /home/{user}/sam-app

command:
sam local invoke HelloWorldFunction -e events/event.json --debug

2023-09-15 13:05:01,864 | Code /home/{user}/sam-app/.aws-sam/build/HelloWorldFunction is not a zip/jar file
2023-09-15 13:05:03,864 | Local image is up-to-date
2023-09-15 13:05:03,879 | Using local image: public.ecr.aws/lambda/python:3.10-rapid-x86_64.

2023-09-15 13:05:03,880 | Mounting /home/{user}/sam-app/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated, inside runtime container
2023-09-15 13:05:04,414 | Starting a timer for 3 seconds for function 'HelloWorldFunction'
START RequestId: e2fbc965-09e9-4b65-a6f5-663807520578 Version: $LATEST
Traceback (most recent call last): Unable to import module 'app': No module named 'app'
END RequestId: e2fbc965-09e9-4b65-a6f5-663807520578
REPORT RequestId: e2fbc965-09e9-4b65-a6f5-663807520578 Init Duration: 0.28 ms Duration: 79.13 ms Billed Duration: 80 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"errorMessage": "Unable to import module 'app': No module named 'app'", "errorType": "Runtime.ImportModuleError", "requestId": "e2fbc965-09e9-4b65-a6f5-663807520578", "stackTrace": []}2023-09-15 13:05:05,953 | Cleaning all decompressed code dirs

Any suggestion?
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants