DEV Community

El Bruno for Microsoft Azure

Posted on • Originally published at on

#Fix – Azure CustomVision Exported to Docker – ImportError: cannot import name ‘soft_unicode’ from ‘markupsafe’

Hi !

In the past days I found a weird error when I export a model created with to docker. The docker image build ok, however, I found this error when I try to run the container.

❯ docker run -p 8090:8090 squirrelscv
Traceback (most recent call last):
  File "", line 7, in <module>
    from flask import Flask, request, jsonify
  File "/usr/local/lib/python3.7/site-packages/flask/", line 14, in <module>
    from jinja2 import escape
  File "/usr/local/lib/python3.7/site-packages/jinja2/", line 12, in <module>
    from .environment import Environment
  File "/usr/local/lib/python3.7/site-packages/jinja2/", line 25, in <module>
    from .defaults import BLOCK_END_STRING
  File "/usr/local/lib/python3.7/site-packages/jinja2/", line 3, in <module>
    from .filters import FILTERS as DEFAULT_FILTERS # noqa: F401
  File "/usr/local/lib/python3.7/site-packages/jinja2/", line 13, in <module>
    from markupsafe import soft_unicode
ImportError: cannot import name 'soft_unicode' from 'markupsafe' (/usr/local/lib/python3.7/site-packages/markupsafe/

Enter fullscreen mode Exit fullscreen mode

error running the docker image exported from custom vision

And hey, it took me sometime to understand and find a way to solve the error. If you want to avoid searching and reading, you only need to read this Issue

[Python] Release 4.21.0 broke multiple Google Cloud client libraries (“TypeError: Descriptors cannot not be created directly.”) #10051

And hey, the solution is to make a downgrade to some packages in the docker image definition. The following docker definition, adds the 2 necessary packages to make it work.

FROM python:3.7-slim

RUN pip install -U pip

# custom packages version to avoid custom vision error
RUN pip install markupsafe==2.0.1
RUN pip install protobuf==3.20.1

RUN pip install --no-cache-dir numpy~=1.17.5 tensorflow~=2.0.2 flask~=1.1.2 pillow~=7.2.0
RUN pip install --no-cache-dir mscviplib==2.200731.16

COPY app /app

# Expose the port

# Set the working directory

# Run the flask server for the endpoints
CMD python -u

Enter fullscreen mode Exit fullscreen mode

And, now we can run the images !

docker image running

Happy coding!


El Bruno

More posts in my blog

Top comments (0)