streamlit-pydantic icon indicating copy to clipboard operation
streamlit-pydantic copied to clipboard

Code for simple for throws PydanticImportError

Open SnoozingSimian opened this issue 1 year ago • 5 comments

Code for Simple Form in README.md cannot be run

I was starting out with streamlit-pydantic and tried to run the code for the simple form given in the README.md, but I encountered an import error while running the simple example.

2023-07-21 20:33:40.546 Uncaught app exception
Traceback (most recent call last):
  File "/Users/ayangangopadhyay/Documents/Projects/Expenditure_Dashboard/.venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
  File "/Users/ayangangopadhyay/Documents/Projects/Expenditure_Dashboard/simple_form.py", line 4, in <module>
    import streamlit_pydantic as sp
  File "/Users/ayangangopadhyay/Documents/Projects/Expenditure_Dashboard/.venv/lib/python3.10/site-packages/streamlit_pydantic/__init__.py", line 9, in <module>
    from .settings import StreamlitSettings
  File "/Users/ayangangopadhyay/Documents/Projects/Expenditure_Dashboard/.venv/lib/python3.10/site-packages/streamlit_pydantic/settings.py", line 4, in <module>
    from pydantic import BaseSettings
  File "/Users/ayangangopadhyay/Documents/Projects/Expenditure_Dashboard/.venv/lib/python3.10/site-packages/pydantic/__init__.py", line 207, in __getattr__
    return _getattr_migration(attr_name)
  File "/Users/ayangangopadhyay/Documents/Projects/Expenditure_Dashboard/.venv/lib/python3.10/site-packages/pydantic/_migration.py", line 288, in wrapper
    raise PydanticImportError(
pydantic.errors.PydanticImportError: `BaseSettings` has been moved to the `pydantic-settings` package. See https://docs.pydantic.dev/2.0.3/migration/#basesettings-has-moved-to-pydantic-settings for more details.

For further information visit https://errors.pydantic.dev/2.0.3/u/import-error

The code I am trying to run is exactly the one given for the simple form -

import streamlit as st
from pydantic import BaseModel
import streamlit_pydantic as sp

class ExampleModel(BaseModel):
    some_text: str
    some_number: int
    some_boolean: bool

data = sp.pydantic_form(key="my_form", model=ExampleModel)
if data:
    st.json(data.json())

Expected behaviour:

Rendering a simple form with streamlit-pydantic.

Steps to reproduce the issue:

  1. Create a .venv
  2. Install required dependencies
  3. Create the simple_form.py file using the above code or copying it from the README.md
  4. streamlit run simple_form.py

-->

Technical details:

  • Host Machine OS (Windows/Linux/Mac): Mac
  • Browser (Chrome/Firefox/Safari): Arc/Mozilla
  • Python Version: 3.10.8
  • streamlit-pydantic Version: 0.6.0
  • streamlit: 1.24.1

Please let me know if any further details are required, and I will be happy to provide them. Thanks!

SnoozingSimian avatar Jul 21 '23 15:07 SnoozingSimian

@SnoozingSimian

Fixed here: #35. Not yet released.

Short term work around - downgrade to pydantic 1.10.11.

frederickmannings avatar Jul 23 '23 15:07 frederickmannings

Duplicate of #33

szabi avatar Aug 02 '23 12:08 szabi

I had the same problem. I fixed through the following steps: (If you like to align with the pydantic 2.4.2.) First, install the following package:

pip install pydantic-settings

change the line 4 from:

from pydantic import BaseSettings

to the following:

from pydantic_settings import BaseSettings

That will fix the problem for you. With that being said, there might be additional misalignment problems elsewhere. Hence it might be a better choice to use the official solutions.

shengxio avatar Oct 24 '23 23:10 shengxio

@SnoozingSimian

Fixed here: #35. Not yet released.

Short term work around - downgrade to pydantic 1.10.11.

@frederickmannings when will this be released? This project looks promising, but given that the rest of my system uses pydantic > 2, this is a blocker.

tamir-alltrue-ai avatar Feb 07 '24 00:02 tamir-alltrue-ai

Any update on a release with an official fix? The import error is still present with the current release on pypi.

aryan-jain avatar Apr 02 '24 19:04 aryan-jain