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

ImportError: cannot import name 'escape' from 'jinja2' #3791

Closed
arber-od opened this issue Mar 31, 2022 · 8 comments
Closed

ImportError: cannot import name 'escape' from 'jinja2' #3791

arber-od opened this issue Mar 31, 2022 · 8 comments

Comments

@arber-od
Copy link

Description:

During sam build we faced an issue:
ImportError: cannot import name 'escape' from 'jinja2' (/usr/local/lib/python3.7/dist-packages/jinja2/init.py)

Steps to reproduce:

sam build

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

  1. OS: alpine
  2. sam --version: 1.43.0-py3-none-any
  3. AWS region: eu-central-1

$ sam build
SAM CLI now collects telemetry to better understand customer needs.
You can OPT OUT and disable telemetry collection by setting the
environment variable SAM_CLI_TELEMETRY=0 in your shell.
Thanks for your help!
Learn More: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-telemetry.html
Traceback (most recent call last):
File "/usr/local/bin/sam", line 10, in
sys.exit(cli())
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 829, in call
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/click/decorators.py", line 73, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/samcli/lib/telemetry/metric.py", line 166, in wrapped
raise exception # pylint: disable=raising-bad-type
File "/usr/local/lib/python3.7/dist-packages/samcli/lib/telemetry/metric.py", line 124, in wrapped
return_value = func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/samcli/lib/utils/version_checker.py", line 41, in wrapped
actual_result = func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/samcli/cli/main.py", line 87, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/samcli/commands/build/command.py", line 192, in cli
build_image,
File "/usr/local/lib/python3.7/dist-packages/samcli/commands/build/command.py", line 220, in do_cli
from samcli.commands.build.build_context import BuildContext
File "/usr/local/lib/python3.7/dist-packages/samcli/commands/build/build_context.py", line 13, in
from samcli.lib.providers.sam_api_provider import SamApiProvider
File "/usr/local/lib/python3.7/dist-packages/samcli/lib/providers/sam_api_provider.py", line 6, in
from samcli.lib.providers.api_collector import ApiCollector
File "/usr/local/lib/python3.7/dist-packages/samcli/lib/providers/api_collector.py", line 10, in
from samcli.local.apigw.local_apigw_service import Route
File "/usr/local/lib/python3.7/dist-packages/samcli/local/apigw/local_apigw_service.py", line 11, in
from flask import Flask, request
File "/usr/local/lib/python3.7/dist-packages/flask/init.py", line 14, in
from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (/usr/local/lib/python3.7/dist-packages/jinja2/init.py)

@hawflau
Copy link
Contributor

hawflau commented Mar 31, 2022

That's probably the same issue - pallets/flask#4494
Looking into it.

@hawflau
Copy link
Contributor

hawflau commented Mar 31, 2022

@arber-od meanwhile, would you be able to install SAM CLI using Linux installer?

@hawflau hawflau added stage/needs-investigation Requires a deeper investigation type/bug labels Mar 31, 2022
@ryankazokas
Copy link

I am having the same issue within our pipelines environment.

Docker image: image: python:3.7.4
aws sam cli - 1.43.0
aws-cli/1.22.88 Python/3.7.4 Linux/5.13.0-1017-aws botocore/1.24.33

It seems to be intermittent as if I re-run the build once or twice it works fine. I also ran the same container/commands locally with my code and did not have an issue, so I'm a little confused on what we could do to fix this. I ran into the same issue mentioned above pallets/flask#4494 but it seems like something needs to be fixed in the sam cli build.

If it helps, this started happening a week or so ago.

@qingchm
Copy link
Contributor

qingchm commented Apr 8, 2022

Thanks for reaching out! I believe this issue is due to the newest versions of jinja2 deprecated the "escape" as referenced in https://jinja.palletsprojects.com/en/3.1.x/changes/#version-3-1-0, https://stackoverflow.com/questions/71718167/importerror-cannot-import-name-escape-from-jinja2 also has some context on this to read about, we will look into how to update our dependency versions to prevent this. Thanks for reporting this and we for sure will try to address this ASAP!

@qingchm
Copy link
Contributor

qingchm commented Apr 8, 2022

Meanwhile I believe you can try to install through other methods, or installing in a separate virtual env to see if you can get yourself unblocked for now! Let me know if that works for you!

@ryankazokas
Copy link

Thanks @qingchm . Not a python expert, so there might be a better way here but manually overriding the dependency version of jinja2 works as a stop gap for us until this gets' fixed on the sam cli side.

pip install Jinja2==2.11.3

sjpalf added a commit to hmrc/aws-lambda-clickhouse-config-in-zookeeper that referenced this issue Jun 7, 2022
workaround for issue with sam dependencies tracked in aws/aws-sam-cli#3791
sjpalf added a commit to hmrc/aws-lambda-ec2-launch-checks that referenced this issue Jun 7, 2022
workaround for issue with sam dependencies tracked in aws/aws-sam-cli#3791
sjpalf added a commit to hmrc/aws-lambda-telescope-msk that referenced this issue Jun 7, 2022
workaround for issue with sam dependencies tracked in aws/aws-sam-cli#3791
ghost pushed a commit to hmrc/aws-lambda-clickhouse-config-in-zookeeper that referenced this issue Jun 7, 2022
ghost pushed a commit to hmrc/aws-lambda-telescope-msk that referenced this issue Jun 7, 2022
@qingchm
Copy link
Contributor

qingchm commented Jun 30, 2022

I believe that this is fixed in the new 1.53.0 release. Please have a try and if you still have any questions or issues feel free to re-open the issue!

@qingchm qingchm closed this as completed Jun 30, 2022
@BrianCollinss
Copy link

BrianCollinss commented Aug 16, 2022

I am using SAM 1.54.0 and I get this error. I tried pip install Jinja2==2.11.3 to no avail.

184798923-ce7fecbd-fc76-44fb-bf74-5489b4c3021b

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

6 participants