diff --git a/reflex/config.py b/reflex/config.py index 0955d24b9..ef48895ea 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -812,18 +812,19 @@ def get_config(reload: bool = False) -> Config: if reload and constants.Config.MODULE in sys.modules: del sys.modules[constants.Config.MODULE] - sys.path.insert(0, os.getcwd()) - # only import the module if it exists. If a module spec exists then - # the module exists. - spec = find_spec(constants.Config.MODULE) - cwd = Path(os.getcwd()).resolve() - if not spec or ( - # Config module path must be relative to the working directory. - (spec.origin is not None) - and not Path(spec.origin).resolve().is_relative_to(cwd) - ): - # we need this condition to ensure that a ModuleNotFound error is not thrown when - # running unit/integration tests or during `reflex init`. - return Config(app_name="") - rxconfig = importlib.import_module(constants.Config.MODULE) - return rxconfig.config + sys_path = list(sys.path) + sys.path.clear() + sys.path.append(os.getcwd()) + try: + # only import the module if it exists. If a module spec exists then + # the module exists. + spec = find_spec(constants.Config.MODULE) + if not spec: + # we need this condition to ensure that a ModuleNotFound error is not thrown when + # running unit/integration tests or during `reflex init`. + return Config(app_name="") + rxconfig = importlib.import_module(constants.Config.MODULE) + return rxconfig.config + finally: + sys.path.clear() + sys.path.extend(sys_path)