【问题标题】:Template DataFlow with multiple Python dependencies具有多个 Python 依赖项的模板数据流
【发布时间】:2021-06-02 04:45:43
【问题描述】:

我正在尝试从具有多个文件依赖项的管道在 Python 中创建模板 DataFlow。

这是项目结构:

root
|
----> project_dir
      |
      ----> __init__.py
      ----> main.py
      ----> setup.py
      utils
      |
      ----> functions.py
      ----> __init__.py

在 main.py 文件中有导入行:

from project_dir.utils.functions import something

我的 setup.py 文件包含(如建议的 here ):

package_dir={'.': ''},
packages=setuptools.find_packages()
            

模板文件生成没有问题,但每次我尝试在 DataFlow 上执行它时都会收到以下错误:

ImportError: No module named 'project_dir'

有人可以帮帮我吗? 提前致谢!

【问题讨论】:

  • utils 目录是在“root/utils”还是“root/project_dir/utils”?该图使它看起来像第一个,但您的导入意味着第二个。
  • 我也有同样的问题,你解决了吗?
  • @DarioB 我刚刚发布了问题的答案,希望对你有帮助!

标签: python google-cloud-platform google-cloud-dataflow apache-beam


【解决方案1】:

为了解决这个问题,我改用了以下结构:

root
|
----> project_dir
  |
  ----> __init__.py
  ----> main.py
  utils
  |
  ----> functions.py
  ----> __init__.py
  setup.py
  installment_requirements.txt

这是我的 setup.py 文件:

import setuptools

requires = [
    'google-cloud-storage==1.36.1',
    'pysftp==0.2.9'
]

setuptools.setup(
    name='name',
    version='0.0.1',
    install_requires=requires,
    packages=setuptools.find_packages()
)

然后我使用安装需求的 Cloudbuild 创建模板并使用模板创建参数执行管道:

steps:
  - name: 'python:3.8-slim'
    args: ['pip', 'install', '--upgrade', 'pip']
    waitFor: ['-']
    id: 'upgrade-pip'
  - name: 'python:3.8-slim'
    args: ['pip', 'install', '-r', './installment_requirements.txt', '--user']
    waitFor: ['upgrade-pip']
    id: 'install-requirements'
  - name: 'python:3.8-slim'
    args: ["python", "./project_dir/main.py"]
    env: ['PYTHONPATH=./', 'DEPLOYMENT_ENVIRONMENT=${_DEPLOYMENT_ENVIRONMENT}']
    waitFor: ['install-requirements']
    id: 'create-df-template

文件 installment_requirements.txt 是使用 pip freeze 导出的,以便在模板创建期间安装依赖项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多