具有多个Python依赖项的模板数据流

问题描述 投票:0回答:2

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

这是项目结构:

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'

有人可以帮助我吗? 预先感谢!

python google-cloud-platform google-cloud-dataflow apache-beam
2个回答
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 创建模板,该 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 导出的,以便在模板创建过程中安装要安装的依赖项。


0
投票

现在 GoogleCloudPlatform github 存储库上有一个关于此的很好的示例: https://github.com/GoogleCloudPlatform/solutions-dataflow-python-multiple-files

© www.soinside.com 2019 - 2024. All rights reserved.