Closed pongpatapee closed 5 months ago
This means that Pydantic settings somehow previously set persistent environment variables that are not being overridden with the .env ones. I am unsure if this is expected behavior, but this feels like a bug to me, please correct me if I'm wrong.
pydantic-settings
never set the env variables. It only reads environment variables and builds the settings model.
Additionally, Pydantic is not validating the .env file path. For example, I can set the .env_file in model_config to any random string, and as long as the environment variables the model is expecting exist, no errors will be thrown.
Yes, if the env file does not exist, pydantic-settings
ignore the file and doesn't load the values from the file. no error will be raised for this.
env variables that load by env settings source have priority over dotenv file values. it is mentioned in the doc
Even when using a dotenv file, pydantic will still read environment variables as well as the dotenv file, environment variables will always take priority over values loaded from a dotenv file.
you can change the priority if you want.
Ah I see, thanks for the response, that explains why the .env
file was ignored.
pydantic-settings never set the env variables. It only reads environment variables and builds the settings model.
My reproducibility steps aside, I never set those environment variables myself, they were all originally from the .env
file. I thought pydantic was responsible for this, but I can't seem to reproduce the issue in isolation.
It is possible that vscode and/or conda were responsible for setting those environment variables. Doing env | grep DB_
shows my environment variables as part of VSCODE_ENV_REPLACE=CONDA_DEFAULT_ENV
in addition to the isolated variables themselves.
I'll close the issue for now, if I can identify the root cause coming from pydantic I can re-open the issue then.
Thanks!
Initial Checks
Description
Bug
Context: I was using Pydantic's BaseSettings to configure some Database settings.
The issue I faced was no matter what values I updated in the
.env
, the output of the settings always remained the same. This led me to:.env
.env
file.env_file
field in model_config to random paths/stringsAll of which resulted in the same values being outputted (The cached values that I have set before which were not updating)
My initial bug report was going to be that Pydantic is reading off a non-existent
.env
file. However, after more debugging, I realized that these environment variables were persistently set for my current directory (i.e., creating a new shell session did not fix the issue).This means that Pydantic settings somehow previously set persistent environment variables that are not being overridden with the .env ones. I am unsure if this is expected behavior, but this feels like a bug to me, please correct me if I'm wrong.
Additionally, Pydantic is not validating the
.env
file path. For example, I can set the.env_file
inmodel_config
to any random string, and as long as the environment variables the model is expecting exist, no errors will be thrown.E.g., this throws no errors
How to reproduce
env_file
env_file
.env
path, updated values will not be populated given a valid.env
path.Example Code
Python, Pydantic & OS Version