I am trying to read variables from .env file, that looks like this:
A = 'aaa'
B = 'bbb'
To read variables I create Settings() class in test_vars.py:
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
a: str
b: str
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')
config = Settings()
These two files are in the same directory.
When trying to create Settings() class, I get this error:
pydantic_core._pydantic_core.ValidationError: 2 validation errors for Settings
a
Field required [type=missing, input_value={}, input_type=dict]
Probably I should edit .env somehow, but I'm not sure.
You code works fine.
When you run this code it will look for the .env
file in the current working directory. So, you should run this code from the directory, where your .env
file is placed. Or you can specify correct path to the env
file from your working directory.