【问题标题】:Airfllow cannot find json file气流找不到 json 文件
【发布时间】:2020-07-06 01:54:59
【问题描述】:

我的 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 Webserver 中我遇到了这个错误:

FileNotFoundError: [Errno 2] 没有这样的文件或目录:'~/airflow/dags/support/gqp.json'

但我可以成功地使用此路径在我的 bash 上捕获文件内容。 我在堆栈中阅读了这两个问题,[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,但都不起作用! 有谁知道如何解决这个问题?

【问题讨论】:

    标签: python airflow


    【解决方案1】:

    如果你尝试:

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

    你会看到输出是False。这是因为 python 不会将 ~ 扩展到您的用户主目录。您可以使用os.path.expanduser 函数来做到这一点:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      • 1970-01-01
      相关资源
      最近更新 更多