gradio icon indicating copy to clipboard operation
gradio copied to clipboard

Installation problems related to jinja2/markupsafe

Open vadimkantorov opened this issue 2 years ago • 5 comments

Describe the bug

Maybe because of some underconstrained dependency versions, to make run gradio on Python 3.8.10 I had to:

python3 -m pip install typing_extensions --upgrade --user
python3 -m pip install markupsafe==2.0.1 --upgrade --user

# https://stackoverflow.com/questions/72191560/importerror-cannot-import-name-soft-unicode-from-markupsafe

# With newer versions of markupsafe: 2.1.3 etc, jinja2 does not import successfully, so maybe jinja2 should be pinned to a more recent version

Otherwise I was getting errors:

Traceback (most recent call last):
  File "app.py", line 1, in <module>
    import gradio as gr
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/__init__.py", line 3, in <module>
    import gradio.components as components
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/components/__init__.py", line 1, in <module>
    from gradio.components.annotated_image import AnnotatedImage
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/components/annotated_image.py", line 12, in <module>
    from gradio import utils
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/utils.py", line 41, in <module>
    from pydantic import BaseModel, parse_obj_as
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/pydantic/__init__.py", line 13, in <module>
    from . import dataclasses
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/pydantic/dataclasses.py", line 11, in <module>
    from ._internal import _config, _decorators, _typing_extra
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/pydantic/_internal/_config.py", line 9, in <module>
    from ..config import ConfigDict, ExtraValues, JsonEncoder, JsonSchemaExtraCallable
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/pydantic/config.py", line 9, in <module>
    from .deprecated.config import BaseConfig
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/pydantic/deprecated/config.py", line 6, in <module>
    from typing_extensions import Literal, deprecated
ImportError: cannot import name 'deprecated' from 'typing_extensions' (/home/vadimkantorov/.local/lib/python3.8/site-packages/typing_extensions.py)

Traceback (most recent call last):
  File "app.py", line 1, in <module>
    import gradio as gr
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/__init__.py", line 3, in <module>
    import gradio.components as components
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/components/__init__.py", line 1, in <module>
    from gradio.components.annotated_image import AnnotatedImage
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/components/annotated_image.py", line 13, in <module>
    from gradio.components.base import IOComponent, _Keywords
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/components/base.py", line 29, in <module>
    from gradio.blocks import Block, BlockContext
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/blocks.py", line 27, in <module>
    from gradio import (
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/networking.py", line 18, in <module>
    from gradio.routes import App
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/gradio/routes.py", line 36, in <module>
    from fastapi.templating import Jinja2Templates
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/fastapi/templating.py", line 1, in <module>
    from starlette.templating import Jinja2Templates as Jinja2Templates  # noqa
  File "/home/vadimkantorov/.local/lib/python3.8/site-packages/starlette/templating.py", line 11, in <module>
    import jinja2
  File "/usr/lib/python3/dist-packages/jinja2/__init__.py", line 33, in <module>
    from jinja2.environment import Environment, Template
  File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 15, in <module>
    from jinja2 import nodes
  File "/usr/lib/python3/dist-packages/jinja2/nodes.py", line 23, in <module>
    from jinja2.utils import Markup
  File "/usr/lib/python3/dist-packages/jinja2/utils.py", line 656, in <module>
    from markupsafe import Markup, escape, soft_unicode
ImportError: cannot import name 'soft_unicode' from 'markupsafe' (/home/vadimkantorov/.local/lib/python3.8/site-packages/markupsafe/__init__.py)

Have you searched existing issues? 🔎

  • [X] I have searched and found no existing issues

Reproduction

N/A

Screenshot

No response

Logs

No response

System Info

N/A

Severity

I can work around it

vadimkantorov avatar Aug 09 '23 15:08 vadimkantorov

Thanks @vadimkantorov. Do you know what versions of markupsafe and typing_extensions you were using before? Would allow us to narrow the range the right versions.

abidlabs avatar Aug 09 '23 19:08 abidlabs

typing-extensions-4.4.0 was bad, typing-extensions-4.7.1 is good MarkupSafe-2.1.3 was bad, 2.0.1 is good - so not very satisfying, as the most recent version fails here

this is for jinja2 version 2.10.1

if constraints can't be used, maybe at least it could catch these problems (e.g. by trying first from markupsafe import soft_unicode and from typing_extensions import deprecated and if import pydantic/import jinja2 fail and also previous guard imports fail, then give an installation advice)

vadimkantorov avatar Aug 09 '23 19:08 vadimkantorov

I window OS gradio is working well. But while working on Ubuntu 18.04 with python 3.6.9 within a virtual environment, I am facing one issue while running the python file. After trying the above methods still getting the following errors. Can anyone suggest some solutions. Screenshot from 2023-10-12 19-57-31

mlswagat avatar Oct 12 '23 14:10 mlswagat

with python 3.6.9

@mlswagat Python 3.6 and 3.7 have been deprecated! Please upgrade to +3.8. Gradio only supports +3.8

freddyaboulton avatar Oct 12 '23 14:10 freddyaboulton

The root cause with your issue stems from a dependency management conflict between two software ecosystems. According to the error message, your 'markupsafe' was installed using 'pip' at the path /home/vadimkantorov/.local/lib/python3.8/site-packages/markupsafe/. The 'jinja2' that depends on it was installed using 'apt' at the path /usr/lib/python3/dist-packages/jinja2. The versions of the two are not compatible. In fact, there is a version of 'markupsafe' in the system that 'jinja2' correctly depends on, but the Python interpreter prioritizes the 'markupsafe' installed by 'pip', leading to this issue. There are typically two solutions to this problem: (1) Use 'pip' to uninstall markupsafe, and then reinstall it using 'apt', or uninstall 'jinja2' using 'apt' and reinstall it using 'pip'. (2) Use Python's imp module to customize the path and import the 'markupsafe' from the 'apt' path before importing 'jinja2'. An example is as follows:

import imp
path = ['/usr/lib/python3/dist-packages']
fp, pathname, description = imp.find_module('markupsafe', path)
imp.load_module("markupsafe", fp, pathname, description)

Hope my diagnosis is helpful to you! @vadimkantorov

cse0001 avatar Feb 01 '24 20:02 cse0001