python - 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
解决方案
该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
我做了一些测试,错误消失了。我希望您发现上述信息有用。
推荐阅读
- python - 每次迭代都基于 iloc[0 : current] 计算行
- javascript - 为 JQgrid 中的复选框赋值
- mysql - 每次 XAMPP 重启后 Mysql 表崩溃
- c++ - 将窗口大小从还原更改为最大化会导致某些内容出现在另一个内容之上
- unity3d - unity拖拽世界位置
- networking - 如何使用 mitmproxy 将 HTTPS 流量重定向到本地 HTTP 服务器?
- ruby-on-rails - 如何使用 rswag 在查询 api rspec 测试中传递字符串数组?
- css - CSS背景固定+封面拉伸图片(未正确覆盖)
- unity3d - unity粒子轮廓着色器
- flutter - 返回图表时出现索引错误