Skip to content

Commit

Permalink
[ENG-4647] Fix env_file handling
Browse files Browse the repository at this point in the history
* Import dotenv.load_dotenv early to avoid ImportError while loading
  rxconfig.py
* Read ENV_FILE from the environment explicitly.

fix #4803
  • Loading branch information
masenf committed Feb 12, 2025
1 parent 289d10d commit db7c392
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions reflex/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
get_origin,
)

from reflex_cli.constants.hosting import Hosting
from typing_extensions import Annotated, get_type_hints

from reflex import constants
from reflex.base import Base
from reflex.utils import console
from reflex.utils.exceptions import ConfigError, EnvironmentVarValueError
from reflex.utils.types import GenericType, is_union, value_inside_optional

Expand All @@ -36,11 +40,11 @@
except ModuleNotFoundError:
import pydantic

from reflex_cli.constants.hosting import Hosting

from reflex import constants
from reflex.base import Base
from reflex.utils import console
try:
from dotenv import load_dotenv # pyright: ignore [reportMissingImports]
except ImportError:
load_dotenv = None


class DBConfig(Base):
Expand Down Expand Up @@ -793,16 +797,15 @@ def update_from_env(self) -> dict[str, Any]:
Returns:
The updated config values.
"""
if self.env_file:
try:
from dotenv import load_dotenv # pyright: ignore [reportMissingImports]

# load env file if exists
load_dotenv(self.env_file, override=True)
except ImportError:
env_file = self.env_file or os.environ.get("ENV_FILE", None)
if env_file:
if load_dotenv is None:
console.error(
"""The `python-dotenv` package is required to load environment variables from a file. Run `pip install "python-dotenv>=1.0.1"`."""
)
else:
# load env file if exists
load_dotenv(env_file, override=True)

updated_values = {}
# Iterate over the fields.
Expand Down

0 comments on commit db7c392

Please sign in to comment.