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.

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

See original GitHub issue

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 <module> 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 <module> 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 <module> 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 <module> 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 <module> from flask import Flask, request File “/usr/local/lib/python3.7/dist-packages/flask/init.py”, line 14, in <module> from jinja2 import escape ImportError: cannot import name ‘escape’ from ‘jinja2’ (/usr/local/lib/python3.7/dist-packages/jinja2/init.py)

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ryankazokascommented, Apr 12, 2022

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

2reactions
ryankazokascommented, Apr 5, 2022

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 https://github.com/pallets/flask/issues/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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ImportError: cannot import name 'escape' from 'jinja2'
To fix this issue, simply update to the newer version of Flask V2.X.X in your requirements.txt where Flask no longer uses the escape...
Read more >
ImportError: cannot import name 'escape' from 'jinja2'
The "ImportError: cannot import name 'escape' from 'jinja2'" is caused because the escape function was removed in version 3.1.0 of jinja2 .
Read more >
Importerror: cannot import name 'escape' from 'jinja2' ( Solved )
importerror: cannot import name 'escape' from 'jinja2' error occurs because of restructuring of jinja internal modules in a recent release.
Read more >
ImportError: cannot import name 'escape' from 'jinja2 ... - GitHub
Sounds like a library you use is attempting to do from jinja2 import escape , which was previously deprecated and now removed.
Read more >
How to Fix ImportError: cannot import name 'escape' from 'jinja2'
The ImportError: cannot import name 'escape' from 'jinja2' error occurs when you try to import the escape function from the jinja2 module.
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