python 版本 jaeger-client 导入失败 jaeger-client-python

环境为:

OS: ubuntu18.04

Python: 3.6

问题原因:

尝试使用 jaeger-client-python,官方给出的示例(https://github.com/jaegertracing/jaeger-client-python ReadMe文件中所述),出现了下面错误

Traceback (most recent call last):
  File "jaeger.py", line 3, in <module>
    from jaeger_client import Config
  File "/usr/local/lib/python3.6/dist-packages/jaeger_client/__init__.py", line 27, in <module>
    from .config import Config  # noqa
  File "/usr/local/lib/python3.6/dist-packages/jaeger_client/config.py", line 26, in <module>
    from .reporter import (
  File "/usr/local/lib/python3.6/dist-packages/jaeger_client/reporter.py", line 32, in <module>
    from jaeger_client.thrift_gen.agent import Agent
  File "/usr/local/lib/python3.6/dist-packages/jaeger_client/thrift_gen/agent/Agent.py", line 13, in <module>
    from .ttypes import *
  File "/usr/local/lib/python3.6/dist-packages/jaeger_client/thrift_gen/agent/ttypes.py", line 12, in <module>
    import jaeger.ttypes
  File "/mnt/d/code/doc2/13_jaeger/jaeger.py", line 3, in <module>
    from jaeger_client import Config
ImportError: cannot import name 'Config'  

经过查找发现是在 thrift 生成的文件中导入包出错

解决方案:重新打包 .whl 安装包

step1:下载 jaeger-client-python 源码

step2:修改代码如下

文件1:./jaeger_client/thrift_gen/agent/ttypes.py 中,找到

import jaeger.ttypes

修改为:

from jaeger_client.thrift_gen.jaeger import ttypes as jaeger_ttypes

文件2:./jaeger_client/thrift_gen/agent/Agent.py 中,找到

...
  thrift_spec = (
    None, # 0
    (1, TType.STRUCT, 'batch', (jaeger.ttypes.Batch, jaeger.ttypes.Batch.thrift_spec), None, ), # 1
  )
...

      if fid == 1:
        if ftype == TType.STRUCT:
          self.batch = jaeger.ttypes.Batch()
          self.batch.read(iprot)
        else:
...

修改其中的 jaeger.ttypes 为 jaeger_ttypes

step3:在代码根目录下执行编译 

python3 setup.py sdist bdist_wheel

完成后,生成的文件在 ./dist 下面,有 .whl 和 tgz 两种格式

原文地址:https://www.cnblogs.com/newguy/p/11804998.html