首页 > 解决方案 > Python 库在数据流中不可见

问题描述

(这不是我之前的问题的重复。我正在尝试一种不同的方法setup.py,它与现有的方法不同requirements.txt

我的项目是这样设置的

.
├── __pycache__
├── eventstream-to-bigq-main.py
└── setup.py

setup.py文件看起来像这样

from setuptools import setup
from setuptools import find_packages

REQUIRED_PACKAGES = [
    "protobuf3-to-dict==0.1.5",
    "protobuf==3.11.2",
]

setup(
    name="eventstream-to-bigq",
    version="1.0",
    install_requires=REQUIRED_PACKAGES,
    packages=find_packages(),
)

最后我想像这样使用这两个库

from google.protobuf.json_format import MessageToDict
from protobuf_to_dict import protobuf_to_dict

def parse_proto(message):
    try:
        dictobj = MessageToDict(message)
        logging.info("google lib", dictobj)
    except Exception as e:
        logging.info("google-lib failed")
        logging.error(e)

    try:
        s = protobuf_to_dict(message)
        logging.info("third party", s)
    except Exception as e:
        logging.info("protobuf-to-dict failed")
        logging.error(e)

即使在此之后,我也收到以下错误

message: "name 'MessageToDict' is not defined"message: "name 'protobuf_to_dict' is not defined"

我在映像启动时监控了日志,我可以看到以下日志行

I 2020-01-31T01:28:22.446626952Z 2020/01/31 01:28:22   Building wheel for protobuf3-to-dict (setup.py): finished with status 'done'

I 2020-01-31T01:28:22.443982764Z 2020/01/31 01:28:22   Building wheel for protobuf3-to-dict (setup.py): started

我也可以看到类似的日志行protobuf。我在这里看到的唯一警告是WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.

即使在所有这些安装之后,我的代码仍然失败

编辑:忘了提到我正在使用这个命令运行它

python -m eventstream-to-bigq-main \
    --input_topic "projects/project_name/topics/topic_name" \
    --job_name "rawdata-to-bigq-2" \
    --output "gs://bucketname/wordcount/outputs" \
    --runner DataflowRunner \
    --project "project_name"  \
    --region "us-central1" \
    --temp_location "gs://bucketname/tmp/" \
    --staging_location "gs://bucketname/staging" \
    --setup_file ./setup.py \
    --streaming True

标签: pythongoogle-cloud-dataflowapache-beamsetuptools

解决方案


protobuf软件包已安装在 Dataflow 工作人员上,您可以在文档中查看。您需要安装的唯一软件包是protobuf3-to-dict.

首先在你的机器上安装包:

pip3 install protobuf3-to-dict

然后,检查您的机器上安装了哪些软件包:

pip freeze > requirements.txt

此命令创建一个requirements.txt文件,其中列出了您机器上安装的所有软件包。请编辑requirements.txt文件并仅保留从 PyPI 安装的包。删除所有与您的代码无关的包。

所以在你的情况下,requirements.txt看起来像这样:

protobuf3-to-dict==0.1.5

使用以下命令行选项运行您的管道,该选项将使用 file 将其他依赖项安装到远程工作人员上:

--requirements_file requirements.txt

我做了一些测试,错误消失了。我希望您发现上述信息有用。


推荐阅读