Airblow找不到json文件

2024-10-05 11:07:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Aitflow流程有以下结构:

dags/mainDag.py
dags/BigQuery/deleteData.py
dags/BigQuery/insertData.py
dags/support/gcp.json
dags/support/__init__py

我的mainDag.py正在调用deleteData.pyinsertData.py,这很有效!但我的问题是:在这两个文件中,我使用的gcp.json如下:

credentialsPath = "~/airflow/dags/support/gqp.json"
bigqueryClient = bigquery.Client.from_service_account_json(credentialsPath)

在Airflow Web服务器中,我有一个错误:

FileNotFoundError: [Errno 2] No such file or directory: '~/airflow/dags/support/gqp.json'

但是我可以使用此路径成功地在bash上对文件内容进行cat。 我在书堆里读了这两个问题,[airflow: how can i put the method for read a json file in a local library和[Airflow - Python file NOT in the same DAG folder,但这两个问题都不起作用! 有人知道如何解决这个问题吗


Tags: 文件pyjsonsupportbigqueryfileairflowgcp
1条回答
网友
1楼 · 发布于 2024-10-05 11:07:11

如果您尝试:

import os
credentialsPath = "~/airflow/dags/support/gqp.json"
print(os.path.isfile(credentialsPath))

您将看到输出为False。这是因为python不会将~扩展到用户主目录。可以使用^{}函数执行此操作:

import os
credentialsPath = os.path.expanduser("~/airflow/dags/support/gqp.json")
print(os.path.isfile(credentialsPath))

现在,这将输出True,因为您的文件路径已使用主目录展开

相关问题 更多 >

    热门问题