spirkaa commented on issue #51422:
URL: https://github.com/apache/airflow/issues/51422#issuecomment-3536132042

   Just upgraded to v3.1.3.
   
   I'm trying to get connection inside `@task.virtualenv`. The  connection is 
defined as an environment variable. I can successfully read it with 
`os.environ["AIRFLOW_CONN_VAULT_DEFAULT"].strip()`, but not with 
`Connection.get("vault_default")`:
   
   ```log
   [2025-11-15 09:56:20] INFO - Executing cmd: uv venv --allow-existing --seed 
--python python --system-site-packages /tmp/venvys70w7pu
   [2025-11-15 09:56:20] INFO - Output:
   [2025-11-15 09:56:21] INFO - Using CPython 3.12.12 interpreter at: 
/usr/python/bin/python
   [2025-11-15 09:56:21] INFO - Creating virtual environment with seed packages 
at: /tmp/venvys70w7pu
   [2025-11-15 09:56:21] INFO -  + pip==25.3
   [2025-11-15 09:56:21] INFO - Executing cmd: uv pip install --python 
/tmp/venvys70w7pu/bin/python -r /tmp/venvys70w7pu/requirements.txt
   [2025-11-15 09:56:21] INFO - Output:
   [2025-11-15 09:56:22] INFO - Using Python 3.12.12 environment at: 
/tmp/venvys70w7pu
   [2025-11-15 09:56:22] INFO - Resolved 9 packages in 292ms
   [2025-11-15 09:56:22] INFO - Downloading psycopg-binary (4.2MiB)
   [2025-11-15 09:56:22] INFO -  Downloading psycopg-binary
   [2025-11-15 09:56:22] INFO - Prepared 9 packages in 287ms
   [2025-11-15 09:56:22] INFO - Installed 9 packages in 24ms
   [2025-11-15 09:56:22] INFO -  + clickhouse-driver==0.2.9
   [2025-11-15 09:56:22] INFO -  + psycopg==3.2.12
   [2025-11-15 09:56:22] INFO -  + psycopg-binary==3.2.12
   [2025-11-15 09:56:22] INFO -  + python-dotenv==1.2.1
   [2025-11-15 09:56:22] INFO -  + pytz==2025.2
   [2025-11-15 09:56:22] INFO -  + stamina==25.1.0
   [2025-11-15 09:56:22] INFO -  + tenacity==9.1.2
   [2025-11-15 09:56:22] INFO -  + typing-extensions==4.15.0
   [2025-11-15 09:56:22] INFO -  + tzlocal==5.3.1
   [2025-11-15 09:56:22] INFO - Use 'pickle' as serializer.
   [2025-11-15 09:56:22] INFO - Executing cmd: /tmp/venvys70w7pu/bin/python 
/tmp/venv-call1zlxzyye/script.py /tmp/venv-call1zlxzyye/script.in 
/tmp/venv-call1zlxzyye/script.out /tmp/venv-call1zlxzyye/string_args.txt 
/tmp/venv-call1zlxzyye/termination.log 
/tmp/venv-call1zlxzyye/airflow_context.json
   [2025-11-15 09:56:22] INFO - Output:
   [2025-11-15 09:56:26] INFO - Traceback (most recent call last):
   [2025-11-15 09:56:26] INFO -   File "/tmp/venv-call1zlxzyye/script.py", line 
63, in <module>
   [2025-11-15 09:56:26] INFO -     res = 
callable_virtualenv(*arg_dict["args"], **arg_dict["kwargs"])
   [2025-11-15 09:56:26] INFO -           
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   [2025-11-15 09:56:26] INFO -   File "/tmp/venv-call1zlxzyye/script.py", line 
31, in callable_virtualenv
   [2025-11-15 09:56:26] INFO -     conn = Connection.get("vault_default")
   [2025-11-15 09:56:26] INFO -            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   [2025-11-15 09:56:26] INFO -   File 
"/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/definitions/connection.py",
 line 225, in get
   [2025-11-15 09:56:26] INFO -     return _get_connection(conn_id)
   [2025-11-15 09:56:26] INFO -            ^^^^^^^^^^^^^^^^^^^^^^^^
   [2025-11-15 09:56:26] INFO -   File 
"/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/context.py",
 line 174, in _get_connection
   [2025-11-15 09:56:26] INFO -     raise AirflowNotFoundException(f"The 
conn_id `{conn_id}` isn't defined")
   [2025-11-15 09:56:26] INFO - airflow.exceptions.AirflowNotFoundException: 
The conn_id `vault_default` isn't defined
   ```
   
   As a workaround i created my own VaultHook to avoid calling `self.connection 
= self.get_connection(vault_conn_id)` and replaced it with
   
   ```python
   conn_var = f"AIRFLOW_CONN_{self.default_conn_name.upper()}"
   self.connection = Connection.from_json(
       conn_id=self.default_conn_name, value=os.environ[conn_var].strip()
   )
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to