简体   繁体   English

Airflow Docker - 没有名为“pyodbc”的模块

[英]Airflow Docker - No module named 'pyodbc'

I have a Python script which uses Pyodbc to work with SQL Server, I'm trying to run this script with an Airflow DAG:我有一个 Python 脚本,它使用 Pyodbc 与 SQL 服务器一起工作,我正在尝试使用 Airflow DAG 运行此脚本:

auction_task = PythonOperator(
  task_id='auction_etl',
  python_callable=get_auction_data,
  dag=dag
)

get_auction_data function in another file: get_auction_data function 在另一个文件中:

import json
import requests
import datetime
import pyodbc
import time

def get_auction_data(config):
    return retrieve_from_api(config)

But I keep getting this error in the UI:但我在 UI 中不断收到此错误:

Broken DAG: [/opt/airflow/dags/auctionds/etl/auction_etl.py] Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/opt/airflow/dags/auctionds/etl/auction_etl.py", line 9, in <module>
    import pyodbc
ModuleNotFoundError: No module named 'pyodbc'

Airflow is running inside a Docker container, I tried to manually install pyodbc in the worker container with no success. Airflow 在 Docker 容器中运行,我尝试在工作容器中手动安装 pyodbc,但没有成功。 Also, if I run python command inside the worker container and run 'import pyodbc' it works with no problem.此外,如果我在工作容器内运行 python 命令并运行“import pyodbc”,它就可以正常工作。 I don't know what I'm missing.我不知道我错过了什么。 Thanks!谢谢!

You need to create a custom image with pyodbc added.您需要创建一个添加了 pyodbc 的自定义图像。 You can see some examples on how to do it in https://airflow.apache.org/docs/docker-stack/build.html#how-to-build-your-own-image including example on how to add new python packages.您可以在https://airflow.apache.org/docs/docker-stack/build.html#how-to-build-your-own-image中看到一些有关如何执行此操作的示例,包括有关如何添加新 Z23EEEB4347BDD26BFC6B77包。

Then you have to use that custom image in your docker deployment (however you deploy the container - might be docker container or K8S for example).然后,您必须在 docker 部署中使用该自定义映像(但是您部署容器 - 例如可能是 docker 容器或 K8S)。 You just need to make your image available to the deployment and make sure your new image is used by it.您只需要使您的映像可用于部署并确保它使用您的新映像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM