【问题标题】:protobuf and python: can protoc generate __init__.py files?protobuf 和 python:protoc 可以生成 __init__.py 文件吗?
【发布时间】:2019-08-18 08:52:05
【问题描述】:

有没有办法让 protoc 在它用来生成 .py 文件的目录中生成空的 __init__.py 文件,以便它们可以用作模块?

【问题讨论】:

    标签: python python-import python-2.x python-module protobuf-python


    【解决方案1】:

    我遇到了同样的问题。我很惊讶 protoc 还没有解决这个问题。此外,protoc 生成的 Python 代码似乎还有其他问题。见https://github.com/protocolbuffers/protobuf/issues/881

    所以目前的答案是。 Protoc 无法生成__init__.py 文件。

    一种选择可能是使用https://github.com/danielgtaylor/python-betterproto 从 .proto 生成 Python 代码(我们的 proto 定义也在子文件夹中构建)。

    我目前正在研究的另一个选项是使用 bash 脚本手动添加一个空的 init.py 文件:Creating empty file in all subfolders

    find <path-to-generated-python-proto> -type d -exec touch {}/__init__.py \;
    

    编辑:我在生成代码的所有文件夹中输入了空的__init__.py,然后我可以使用find_packages 打包Python 模块(因为它只能找到带有__init__.py 的模块)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 2014-02-27
      • 2010-11-25
      • 2019-08-29
      • 2012-02-11
      相关资源
      最近更新 更多