title: Why does AWS sam package causes - Unable to import module '' No module named '' - during sam deploy but sam build does not?
body: <p>The project I am currently working on creates a lambda layer which contains a file called <code>app.py</code>, within this file is a function named <code>lambda_handler</code> which is interest to be used as <code>Handler</code> for whatever lambda function includes the layer. The sam template I use to do this looks as follow:</p>
  <pre><code>Resources:
    LamLayer:
      Type: AWS::Serverless::LayerVersion
      LayerName: !Join
        - ''
        - - 'LamLayer'
        - - !Ref AWS::StackName
      Properties:
        ContentUri: ./lam_layer
        CompatibleRuntimes:
          - python3.8
      Metadata:
        BuildMethod: python3.8
    LamFunction:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: ./lam_function
        Runtime: python3.8
        Handler: app.lambda_handler
        Layers:
          - !Ref LamLayer
        Timeout: 60
        AutoPublishAlias: live
  </code></pre>
  <p>Now although the <code>Handler: app.lambda_handler</code> is not present in the lambda function itself, it is present in the included layer.</p>
  <p>Now after creating this setup I tested it by calling <code>sam build; sam deploy</code> and it successfully deployed and worked. When I called the <code>LamFunction</code> it successfully found the Handler and ran it.</p>
  <p>The problem arises when I push my changes to the CodePipeline we have setup. The build and deploy succeeded but when I now call the <code>LamFunction</code> it throws the following error:</p>
  <pre><code>Unable to import module 'app': No module named 'app'
  </code></pre>
  <p>After debugging this for a while I seem to have narrowed down the problem to the difference in the way I was building vs. how the pipeline is building the project.</p>
  <p>I called: <code>sam build; sam deploy</code></p>
  <p>Whereas the pipeline calls: <code>sam build; sam package --s3-bucket codepipeline-eu-central-1-XXXXXXXXXX --output-template-file packaged-template.yml</code> and then uses the standard pipeline deploy stage to deploy from the S3 bucket.</p>
  <p>But although I think I know that this difference is causing the problem I am not sure what the underlying reason is and what I need to change to fix it ?</p>
  <p>---- EDIT ----</p>
  <p>Here is the <code>buildspec.yml</code> in case this is the culprit:</p>
  <pre><code>version: 0.2
  phases:
    install:
      runtime-versions:
          python: 3.8
    build:
      commands:
        - sam build
        - sam package --s3-bucket codepipeline-eu-central-1-XXXXXXXXXX --output-template-file packaged-template.yml
  artifacts:
    files:
      - packaged-template.yml
  
  </code></pre>