langflow unable to initialize variables from .env file
Discussed in https://github.com/langflow-ai/langflow/discussions/2716
Originally posted by peteBarahon July 16, 2024 Langflow is unable to correctly load environment values from a file. The issue resides with the way the run function in main.py attempts to overwrite local variables via the locals()[] structure. This feature seems to not work correctly on Python 3. The offending code was detected in lines 153 to 156. I corrected the problem by re-evaluating the local variables in question via a separate function. I also utilized the find_dotenv() as a fallback value for a more reliable env_var variable initialization.
I have attached the file for anyone experiencing the same issue. I am not an active member of this group due to time constraints but I hope this humble contribution helps someone out.
Please download it and use a diff tool to compare it to your own so that you can see the changes made. The file extension was changed to .txt to avoid problems with the attachment not uploading. main.py.txt
Thank you so much to everyone involved in this project. It's amazing!
Hello @peteBarahon! I'm a bot here to help you with bug fixes, answer questions, and guide you on how to become a contributor. I'm here to assist you while waiting for a human maintainer.
The issue with Langflow not correctly loading environment values from a .env file due to the use of locals() to overwrite local variables in Python 3 has been identified and corrected by re-evaluating the local variables via a separate function. Additionally, using find_dotenv() as a fallback value for more reliable environment variable initialization has been suggested.
Here is a summary of the changes made to resolve the issue:
-
Re-evaluating Local Variables: Instead of using
locals()to overwrite local variables, a separate function is used to re-evaluate and set the local variables correctly. -
Using
find_dotenv(): This function is used as a fallback to ensure that the.envfile is reliably located and loaded.
You can review the corrected __main__.py file provided by the user in the discussion here.
For reference, the original approach to load environment variables in __main__.py was:
if env_file:
load_dotenv(env_file, override=True)
env_vars = dotenv_values(env_file)
# Define a mapping of environment variables to their corresponding variables and types
env_var_mapping: dict[str, tuple[str, type | Callable[[Any], bool]]] = {
"LANGFLOW_HOST": ("host", str),
"LANGFLOW_PORT": ("port", int),
"LANGFLOW_WORKERS": ("workers", int),
"LANGFLOW_WORKER_TIMEOUT": ("timeout", int),
"LANGFLOW_COMPONENTS_PATH": ("components_path", Path),
"LANGFLOW_LOG_LEVEL": ("log_level", str),
"LANGFLOW_LOG_FILE": ("log_file", Path),
"LANGFLOW_LANGCHAIN_CACHE": ("cache", str),
"LANGFLOW_FRONTEND_PATH": ("path", str),
"LANGFLOW_OPEN_BROWSER": ("open_browser", lambda x: x.lower() == "true"),
"LANGFLOW_REMOVE_API_KEYS": ("remove_api_keys", lambda x: x.lower() == "true"),
"LANGFLOW_BACKEND_ONLY": ("backend_only", lambda x: x.lower() == "true"),
"LANGFLOW_STORE": ("store", lambda x: x.lower() == "true"),
}
# Update variables based on environment variables
for env_var, (var_name, var_type) in env_var_mapping.items():
if env_var in env_vars:
locals()[var_name] = var_type(env_vars[env_var])
This approach was problematic due to the limitations of locals() in Python 3. The corrected approach involves re-evaluating the local variables in a more reliable manner.
For more details on how Langflow handles environment variable loading and initialization, you can refer to the relevant sections in the codebase [1][2].
To continue talking to Dosu, mention @dosu.
+1 Ran into this one too.