question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
srisaithilakcommented, May 12, 2020

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.

0reactions
dominusmicommented, Jun 24, 2021

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}

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - "errorMessage": "Unable to import module 'app': No ...
Now I'm getting another error saying “unable to import module app: no module named requests” My current version of python is 3.10.2 and...
Read more >
Hello World, Unable to import module 'app': No ... - GitHub
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 ...
Read more >
ModuleNotFoundError: no module named Python Error [Fixed]
When you try to import a module in a Python file, Python tries to resolve this module in several ways. Sometimes, Python throws...
Read more >
no module named 'app' | The AI Search Engine You Control
Solution 2. You need to make sure that the project is in your PYTHONPATH. Maybe try to output the sys.path variable at the...
Read more >
__main__ — Top-level code environment — Python 3.11 ...
It's “top-level” because it imports all other modules that the program needs. Sometimes “top-level code” is called an entry point to the application....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found