aws-sam-cli: ImportError: cannot import name 'soft_unicode' from 'markupsafe' in Release 1.38.0

I’m building the sam project on github action and since the upgrade of sam-cli to 1.38.0, i’m getting following error when i build project

Python version: 3.10.2 & 3.8.12 (Tried both versions) Pip version: pip-22.0.3 Sam cli version: 1.38.0

Command: sam build --template ${SAM_TEMPLATE} --use-container

Trace:

Traceback (most recent call last):
  File "/home/runner/work/_temp/setup-sam-43osIE/bin/sam", line 8, in <module>
    sys.exit(cli())
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/click/core.py", line 8[29](https://github.com/mensabrands/dip-app/runs/5243143255?check_suite_focus=true#step:5:29), in __call__
    return self.main(*args, **kwargs)
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/click/decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/lib/telemetry/metric.py", line 166, in wrapped
    raise exception  # pylint: disable=raising-bad-type
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/lib/telemetry/metric.py", line 124, in wrapped
    return_value = func(*args, **kwargs)
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/lib/utils/version_checker.py", line 41, in wrapped
    actual_result = func(*args, **kwargs)
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/cli/main.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/commands/build/command.py", line 174, in cli
    do_cli(
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/commands/build/command.py", line 220, in do_cli
    from samcli.commands.build.build_context import BuildContext
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/commands/build/build_context.py", line 13, in <module>
    from samcli.lib.providers.sam_api_provider import SamApiProvider
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/lib/providers/sam_api_provider.py", line 6, in <module>
    from samcli.lib.providers.api_collector import ApiCollector
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/lib/providers/api_collector.py", line 10, in <module>
    from samcli.local.apigw.local_apigw_service import Route
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/samcli/local/apigw/local_apigw_service.py", line 11, in <module>
    from flask import Flask, request
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/flask/__init__.py", line 14, in <module>
    from jinja2 import escape
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/jinja2/__init__.py", line 12, in <module>
    from .environment import Environment
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/jinja2/environment.py", line 25, in <module>
    from .defaults import BLOCK_END_STRING
  File "/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/jinja2/defaults.py", line 3, in <module>
    from .filters import FILTERS as DEFAULT_FILTERS  # noqa: F[40](https://github.com/mensabrands/dip-app/runs/5243143255?check_suite_focus=true#step:5:40)1
  File "/home/runner/work/_temp/setup-sam-[43](https://github.com/mensabrands/dip-app/runs/5243143255?check_suite_focus=true#step:5:43)osIE/.venv/lib/python3.10/site-packages/jinja2/filters.py", line 13, in <module>
    from markupsafe import soft_unicode
ImportError: cannot import name 'soft_unicode' from 'markupsafe' (/home/runner/work/_temp/setup-sam-43osIE/.venv/lib/python3.10/site-packages/markupsafe/__init__.py)

I revert sam-cli version to 1.37.0 and it worked perfectly.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 67
  • Comments: 44 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Downgrading markupsafe to 2.0.1 fixes the issue on my side. pip install markupsafe==2.0.1

Looks the issue is due to upgrade in MarkupSafe:2.1.0 where they have removed soft_unicode. Release note: https://markupsafe.palletsprojects.com/en/2.1.x/changes/#version-2-1-0

sam-cli:1.37.0 is downloading MarkupSafe:2.0.1 and sam-cli:1.38.0 is downloading MarkupSafe:2.1.0.

@sthadapradit this I did, but it is temproary solution - any idea how to come back to pip install --user --upgrade aws-sam-cli insted pip install --user aws-sam-cli ==1.37.0

same here - seems MarkUoSafe was updated and there is no soft_unicode there any more. How to solve this?

`- step: &build_stage
            name: build
            image: python:3.9
            script:
                - apt-get update && apt-get install -y python3-pip zip 
                - pip install -U awscli
                - aws --version
                - aws configure set default.region $AWS_DEFAULT_REGION
                - aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID_STAGE"
                - aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY_STAGE"
                - pip install --user --upgrade aws-sam-cli
                - ln -s /root/.local/bin/sam /usr/bin/sam
                - sam --version
                - sam build`

pip install --user --upgrade aws-sam-cli - gets last version on Jijnja

Collecting Jinja2<4.0.0,>=2.7 Using cached Jinja2-3.0.3-py3-none-any.whl (133 kB)

yesterday was Collecting Jinja2<4.0.0,>=2.7 Using cached Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)

aws-sam-cli is using an unsupported version of Jinja, they need to update to the latest version. Additionally, they need to read https://hynek.me/articles/semver-will-not-save-you/, then use a tool like pip-tools to pin their dependencies to control when their users get updates. Be sure to run tests with deprecation warnings treated as errors so that these types of changes show up early.

Downgrading markupsafe to 2.0.1 fixes the issue on my side. pip install markupsafe==2.0.1

Thanks a lot. It solved the problem

Looking into this. Though this is why we do not recommend using pip to install and instead use our installers.

@davidism We are stuck in a place that if we pin, customers get upset that they can install with all their other tools (in a single interpreter instead of isolating). If we don’t pin (everything), we run into this things. So it’s a who do we block/make angry situation. Would be great if python (pip) actually solved this instead of each tool having to battle users on how to install or providing ways tools can vend safely through pip.

@SamkeetJainMensa, thanks lot, it’s working with 1.37.0, I did this change in gitaction

  - uses: aws-actions/setup-sam@v1
    with:
      version: 1.37.0

I had this issue with Django using Jinja2. Jinja2 imports soft_unicode from the package causing it to break.

This Issue can be solve using below for steps:

  1. pip install MarkupSafe
  2. pip install markupsafe==2.0.1
  3. pip install --user --upgrade aws-sam-cli
  4. pip install --user --upgrade aws-sam-cli

For anyone finding this organically through search, you might have been searching up the following error:

ImportError: cannot import name ‘soft_unicode’ from ‘markupsafe’

I’m not using aws-sam-cli, but I believe one of my dependencies is using jinja (which uses MarkupSafe). I added an upper bound to the MarkupSafe dependency at the top of my requirements.txt file (if that’s something you’re using).

MarkupSafe<2.1.0

My GitHub Actions workflow uses the requirements file to setup testing, so this made life a bit easier. I imagine @Yop-La’s solution (https://github.com/aws/aws-sam-cli/issues/3661#issuecomment-1044340547) would work just as well.

How I solved it is after all the project dependencies are installed, I do a 'pip uninstall -y markupsafe ’ and then a 'pip install markupsafe==2.0.1 '. Hope this helps someone.

markupsafe

Thank you for your response. I am not using markupsafe directly. How can I pin it?

Like mentioned above in https://github.com/aws/aws-sam-cli/issues/3661#issuecomment-1044340547, you can “pin” it by adding the version number explicitly (markupsafe==2.0.1) in your pip install:

pip install markupsafe==2.0.1

It works the same when you’re installing something else:

pip install something_that_depends_on_markupsafe_transitively markupsafe==2.0.1

See: https://stackoverflow.com/questions/44448925/how-to-pin-transitive-dependencies-with-setup-py-in-python

Why has this been closed? 🤔 This is still an issue with version 1.38.1. because of Flask’s dependency.

Thanks for reporting this issue. SAM CLI v1.38.1 is released which contains the fix for it. As @jfuss mentioned, we would highly recommend using our installers (NativeInstaller for Linux, brew for MacOS and MSI for Windows).

Resolving the issue.

Just ran into this issue installing with aws-actions/setup-sam@v1

easy fix if anyone else sees this though, setup-sam allows you to set version

- uses: aws-actions/setup-sam@v1
   with:
     version: 1.37.0

could you please write the perfect solution for this issue , I’ve tried most of them but no luck

To be honest, there is no perfect solution, because it depends on your environmental configuration.
What is the error on your machine ?