1010
1111# version in pyrec prod
1212config = None
13+ _loaded_env_path = None # Track which env_path was used to load config
1314
1415
1516def find_existing_config ():
@@ -54,14 +55,15 @@ def find_existing_config():
5455
5556def load_env_file (env_path = '.env' ):
5657 """Simple .env file loader compatible with Python 3.6
57-
58- Loads environment variables from .env file and sets them in os.environ
59- ONLY if they don't already exist in os.environ (respecting OS env priority) .
58+
59+ Loads environment variables from .env file and sets them in os.environ.
60+ Local .env file takes precedence over pre-existing environment variables .
6061 """
6162 env_vars = {}
6263 if os .path .exists (env_path ):
6364 try :
6465 with open (env_path , 'r' ) as f :
66+ lines_read = 0
6567 for line in f :
6668 line = line .strip ()
6769 if line and not line .startswith ('#' ) and '=' in line :
@@ -73,11 +75,15 @@ def load_env_file(env_path='.env'):
7375 (value .startswith ("'" ) and value .endswith ("'" )):
7476 value = value [1 :- 1 ]
7577 env_vars [key ] = value
76- # Set in os.environ ONLY if not already set (respects OS env priority)
77- if key not in os .environ :
78- os . environ [ key ] = value
78+ # Always set in os.environ - local . env takes precedence
79+ os .environ [ key ] = value
80+ lines_read += 1
7981 except Exception as e :
80- print (f"Warning: Error loading .env file: { e } " )
82+ print (f"Warning: Error loading .env file from '{ env_path } ': { e } " )
83+ else :
84+ # Only warn about missing custom .env paths, not the default .env
85+ if env_path != '.env' :
86+ print (f"Warning: Custom .env file not found: { env_path } " )
8187 return env_vars
8288
8389def merge_configs (base_config , override_config ):
@@ -90,12 +96,21 @@ def merge_configs(base_config, override_config):
9096 merged [key ] = value
9197 return merged
9298
93- def read_config ():
99+ def read_config (env_path = None ):
94100 global config
95-
96- if config is None :
101+ global _loaded_env_path
102+
103+ # Determine .env path: explicit param > COAIAPY_ENV_PATH env var > default .env
104+ if env_path is None :
105+ env_path = os .getenv ('COAIAPY_ENV_PATH' , '.env' )
106+
107+ # Reload config if env_path changed or config not loaded yet
108+ if config is None or _loaded_env_path != env_path :
97109 # Load .env file first if it exists
98- env_vars = load_env_file ()
110+ env_vars = load_env_file (env_path )
111+ if not env_vars and env_path != '.env' :
112+ # Warn if custom env_path was specified but file not found
113+ print (f"Warning: COAIAPY_ENV_PATH={ env_path } was specified but file not found or empty" )
99114
100115 # Default configuration
101116 config = {
@@ -222,6 +237,9 @@ def get_env_value(env_key, config_value, env_vars_dict=None):
222237 config ["langfuse_base_url" ] = get_env_value ("LANGFUSE_HOST" , config .get ("langfuse_base_url" , "https://us.cloud.langfuse.com" ))
223238 config ["langfuse_auth3" ] = get_env_value ("LANGFUSE_AUTH3" , config .get ("langfuse_auth3" , "" ))
224239
240+ # Track which env_path was used to load this config
241+ _loaded_env_path = env_path
242+
225243 return config
226244
227245
0 commit comments