我有一个使用 apache beam 的 .py 管道导入另一个模块 (.py),这是我的自定义模块。 我有这样的结构:
├── mymain.py
└── myothermodule.py
我像这样在 mymain.py 中导入 myothermodule.py:
import myothermodule
当我在 DirectRuner
上本地运行时,没有问题。
但是当我使用 DataflowRunner
在数据流上运行它时,我有一个错误提示:
ImportError: No module named myothermodule
所以我想知道如果我希望在数据流上运行作业时找到这个模块,我该怎么办?
最佳答案
当您远程运行管道时,您还需要使远程工作人员也可以使用任何依赖项。
为此,您应该将模块文件放入一个 Python 包中,方法是将它放在一个包含 __init__.py
文件的目录中,并创建一个 setup.py。它看起来像这样:
├── mymain.py
├── setup.py
└── othermodules
├── __init__.py
└── myothermodule.py
然后像这样导入它:
from othermodules import myothermodule
然后您可以使用命令行选项 --setup_file ./setup.py
最小的 setup.py 文件如下所示:
import setuptools
setuptools.setup(packages=setuptools.find_packages())
整个设置记录在案here .
可以找到使用它的完整示例 here .
关于python - 数据流/apache 光束 : manage custom module dependencies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51763406/