如何组织thrift client/server的python包?

2024-09-29 21:47:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有很多自定义类型(结构)的thrift接口。我知道如何将python生成的代码组织为一个独立的包。我补充说设置.py到生成代码的根。问题是生成代码的模块无法加载其他生成的模块。组织事物的最佳实践是什么。你知道吗

我尝试使用thrift脚本,因为每个结构只有一个名称空间,或者没有公共名称空间。没关系,问题就在那里。我不喜欢公共名称空间,因为thrift在一个模块中生成所有名称空间。你知道吗

事情是这样的:

├── generatedthrift012
│   └──/Struct1/
│   │            /__init__.py
│   │            /constants.py
│   │            /ttypes.py
│   ├──/MainInterface
│   │            /__init__.py
│   │            /constants.py
│   │            /ttypes.py
│   │            /MyIntreface.py
│   │ 
│   ├──/__init__.py
├── setup.py

以上所有代码设置.py是机器生成的。我不想编辑它。你知道吗

问题是当我像这样导入MyIntreface时:

from generatedthrift012.MainInterface import MyInterface

它无法加载Struct1的类型。

 File "D:\...\ServerEnvelop.py", line 9, in <module>
    from generatedthrift012.MainInterface import MyInterface
  File "build\bdist.win-amd64\egg\generatedthrift012\MainInterface\MyInterface.py", line 15, in <module>
  File "build\bdist.win-amd64\egg\generatedthrift012\MainInterface\ttypes.py", line 14, in <module>
ImportError: No module named Struct1.ttypes

有问题的第14行是from .ttypes import *


Tags: 模块代码frompyimport名称init空间

热门问题