pytorch-lightning: Lightning 2.0 - `ImportError: cannot import name 'Schema' from 'pydantic'`

Bug description

When importing lightning with the new 2.0 release, I get the following error

ImportError: cannot import name 'Schema' from 'pydantic'

How to reproduce the bug

import lightning

Error messages and logs

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/lightning/__init__.py", line 18, in <module>
    from lightning.app import storage  # noqa: E402
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/lightning/app/__init__.py", line 24, in <module>
    from lightning.app import components  # noqa: E402, F401
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/lightning/app/components/__init__.py", line 1, in <module>
    from lightning.app.components.database.client import DatabaseClient
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/lightning/app/components/database/__init__.py", line 1, in <module>
    from lightning.app.components.database.client import DatabaseClient
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/lightning/app/components/database/client.py", line 22, in <module>
    from lightning.app.components.database.utilities import _GeneralModel
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/lightning/app/components/database/utilities.py", line 20, in <module>
    from fastapi import Response, status
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/fastapi/__init__.py", line 5, in <module>
    from .applications import FastAPI
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/fastapi/applications.py", line 3, in <module>
    from fastapi import routing
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/fastapi/routing.py", line 6, in <module>
    from fastapi import params
  File "/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/fastapi/params.py", line 4, in <module>
    from pydantic import Schema
ImportError: cannot import name 'Schema' from 'pydantic' (/Users/thesofakillers/miniconda3/envs/thesis/lib/python3.10/site-packages/pydantic/__init__.cpython-310-darwin.so)

Environment

**

Current environment
* CUDA:
	- GPU:               None
	- available:         False
	- version:           None
* Lightning:
	- lightning:         2.0.0
	- lightning-cloud:   0.5.31
	- lightning-utilities: 0.8.0
	- pytorch-lightning: 2.0.0
	- torch:             2.0.0
	- torchmetrics:      0.11.4
* Packages:
	- accelerate:        0.17.1
	- aiofiles:          22.1.0
	- aiohttp:           3.8.4
	- aiosignal:         1.3.1
	- aiosqlite:         0.18.0
	- aniso8601:         9.0.1
	- anyio:             3.6.2
	- appnope:           0.1.3
	- argon2-cffi:       21.3.0
	- argon2-cffi-bindings: 21.2.0
	- arrow:             1.2.3
	- asttokens:         2.2.1
	- async-timeout:     4.0.2
	- attrs:             22.2.0
	- babel:             2.12.1
	- backcall:          0.2.0
	- beautifulsoup4:    4.11.2
	- black:             23.1.0
	- bleach:            6.0.0
	- blessed:           1.20.0
	- certifi:           2022.12.7
	- cffi:              1.15.1
	- charset-normalizer: 3.1.0
	- click:             8.1.3
	- comm:              0.1.2
	- contourpy:         1.0.7
	- croniter:          1.3.8
	- cycler:            0.11.0
	- dateutils:         0.6.12
	- debugpy:           1.6.6
	- decorator:         5.1.1
	- deepdiff:          6.2.3
	- defusedxml:        0.7.1
	- diffusers:         0.14.0
	- dnspython:         2.3.0
	- email-validator:   1.3.1
	- executing:         1.2.0
	- fastapi:           0.1.17
	- fastjsonschema:    2.16.3
	- filelock:          3.10.0
	- fonttools:         4.39.1
	- fqdn:              1.5.1
	- frozenlist:        1.3.3
	- fsspec:            2023.3.0
	- graphene:          3.2.2
	- graphql-core:      3.2.3
	- graphql-relay:     3.2.0
	- h11:               0.14.0
	- huggingface-hub:   0.13.2
	- idna:              3.4
	- importlib-metadata: 6.0.0
	- inquirer:          3.1.3
	- ipykernel:         6.21.3
	- ipython:           8.11.0
	- ipython-genutils:  0.2.0
	- isoduration:       20.11.0
	- itsdangerous:      2.1.2
	- jedi:              0.18.2
	- jinja2:            3.1.2
	- json5:             0.9.11
	- jsonpointer:       2.3
	- jsonschema:        4.17.3
	- jupyter-client:    8.0.3
	- jupyter-core:      5.2.0
	- jupyter-events:    0.6.3
	- jupyter-server:    2.4.0
	- jupyter-server-fileid: 0.8.0
	- jupyter-server-terminals: 0.4.4
	- jupyter-server-ydoc: 0.6.1
	- jupyter-ydoc:      0.2.3
	- jupyterlab:        3.6.1
	- jupyterlab-code-formatter: 1.5.3
	- jupyterlab-pygments: 0.2.2
	- jupyterlab-server: 2.20.0
	- kiwisolver:        1.4.4
	- lightning:         2.0.0
	- lightning-cloud:   0.5.31
	- lightning-utilities: 0.8.0
	- markdown-it-py:    2.2.0
	- markupsafe:        2.1.2
	- matplotlib:        3.7.1
	- matplotlib-inline: 0.1.6
	- mdurl:             0.1.2
	- mistune:           2.0.5
	- mpmath:            1.3.0
	- multidict:         6.0.4
	- mypy-extensions:   1.0.0
	- nbclassic:         0.5.3
	- nbclient:          0.7.2
	- nbconvert:         7.2.10
	- nbformat:          5.7.3
	- nest-asyncio:      1.5.6
	- networkx:          3.0
	- notebook:          6.5.3
	- notebook-shim:     0.2.2
	- numpy:             1.24.2
	- ordered-set:       4.1.0
	- orjson:            3.8.7
	- packaging:         23.0
	- pandocfilters:     1.5.0
	- parso:             0.8.3
	- pathspec:          0.11.1
	- pexpect:           4.8.0
	- pickleshare:       0.7.5
	- pillow:            9.4.0
	- pip:               23.0.1
	- platformdirs:      3.1.1
	- prometheus-client: 0.16.0
	- prompt-toolkit:    3.0.38
	- psutil:            5.9.4
	- ptyprocess:        0.7.0
	- pure-eval:         0.2.2
	- pycparser:         2.21
	- pydantic:          1.10.6
	- pygments:          2.14.0
	- pyjwt:             2.6.0
	- pyparsing:         3.0.9
	- pyrsistent:        0.19.3
	- python-dateutil:   2.8.2
	- python-editor:     1.0.4
	- python-json-logger: 2.0.7
	- python-multipart:  0.0.6
	- pytorch-lightning: 2.0.0
	- pytz:              2022.7.1
	- pyyaml:            6.0
	- pyzmq:             25.0.1
	- readchar:          4.0.4
	- regex:             2022.10.31
	- requests:          2.28.2
	- rfc3339-validator: 0.1.4
	- rfc3986-validator: 0.1.1
	- rich:              13.3.2
	- safetensors:       0.3.0
	- send2trash:        1.8.0
	- setuptools:        67.6.0
	- setuptools-scm:    7.1.0
	- six:               1.16.0
	- sniffio:           1.3.0
	- soupsieve:         2.4
	- stack-data:        0.6.2
	- starlette:         0.26.1
	- starsessions:      1.3.0
	- sympy:             1.11.1
	- terminado:         0.17.1
	- tinycss2:          1.2.1
	- tomli:             2.0.1
	- torch:             2.0.0
	- torchmetrics:      0.11.4
	- tornado:           6.2
	- tqdm:              4.65.0
	- traitlets:         5.9.0
	- typing-extensions: 4.5.0
	- ujson:             5.7.0
	- uri-template:      1.2.0
	- urllib3:           1.26.15
	- uvicorn:           0.21.1
	- wcwidth:           0.2.6
	- webcolors:         1.12
	- webencodings:      0.5.1
	- websocket-client:  1.5.1
	- websockets:        10.4
	- wheel:             0.37.1
	- y-py:              0.5.9
	- yarl:              1.8.2
	- ypy-websocket:     0.8.2
	- zipp:              3.15.0
* System:
	- OS:                Darwin
	- architecture:
		- 64bit
		- 
	- processor:         i386
	- python:            3.10.9
	- version:           Darwin Kernel Version 19.6.0: Tue Jun 21 21:18:39 PDT 2022; root:xnu-6153.141.66~1/RELEASE_X86_64

**

More info

Seems like I can’t import lightning with the 2.0 release. Complains about not being able to import Schema from pydantic. Only other trace I could fine in the issues page here: https://github.com/Lightning-AI/lightning/pull/14333#issuecomment-1221558023

cc @tchaton

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 21 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I have solved an error by installing higher version of fastapi eg >=0.80.

My poetry resolver decided to use an old version of fastapi, but adding a requirement to pyproject.toml helped.

I just installed Lightning in a clean environment and got

ImportError while loading conftest '/home/carlos/lightning/tests/tests_pytorch/conftest.py'.
../conftest.py:25: in <module>
    import lightning.fabric
../../../src/lightning/__init__.py:18: in <module>
    from lightning.app import storage  # noqa: E402
../../../src/lightning/app/__init__.py:24: in <module>
    from lightning.app import components  # noqa: E402, F401
../../../src/lightning/app/components/__init__.py:15: in <module>
    from lightning.app.components.serve.serve import ModelInferenceAPI
../../../src/lightning/app/components/serve/serve.py:35: in <module>
    fastapi_service = FastAPI()
../../../../venv/lib/python3.8/site-packages/fastapi/applications.py:146: in __init__
    self.middleware_stack: ASGIApp = self.build_middleware_stack()
../../../../venv/lib/python3.8/site-packages/fastapi/applications.py:152: in build_middleware_stack
    debug = self.debug
E   AttributeError: 'FastAPI' object has no attribute 'debug'

Pulling out Lightning

$ python -c "import fastapi; fastapi.FastAPI()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/carlos/venv/lib/python3.8/site-packages/fastapi/applications.py", line 146, in __init__
    self.middleware_stack: ASGIApp = self.build_middleware_stack()
  File "/home/carlos/venv/lib/python3.8/site-packages/fastapi/applications.py", line 152, in build_middleware_stack
    debug = self.debug
AttributeError: 'FastAPI' object has no attribute 'debug'
$ pip list | grep -E 'starlette|fastapi|pydantic'
fastapi                 0.88.0       
pydantic                1.10.7       
starlette               0.26.1      

Uninstalling these 3 and re-installing fixed it:

$ pip uninstall -y starlette fastapi pydantic
...
$ pip install starlette fastapi pydantic
Successfully installed fastapi-0.95.0 pydantic-1.10.7 starlette-0.26.1

But this points that our requirements specify an invalid combination of these 3 dependencies.

The FastAPI docs have some guidance about this: https://fastapi.tiangolo.com/deployment/versions/#about-starlette

fastapi==0.88.0 worked for me, the poetry resolver says anything <0.89.0 should work.

yeah I did the same, let me take a closer look, I’m using this as part of a docker setup so theres a few more moving parts and one of them might not be playing nice. Thanks for letting me know that 0.88 is what is working for you though that helps rule out one part of it