试图从`opencensus.ext.stackdriver`安装和导入`stats_exporter`时进行故障排除

时间:2019-03-25 08:18:03

标签: python opencensus

我正在尝试按照以下指南从stats_exporter安装和使用opencensus.ext.stackdriveropencensus-ext-stackdriver

通过pip安装后:

pip install opencensus-ext-stackdriver

我正在尝试导入它,并且:

from opencensus.ext.stackdriver import stats_exporter as stackdriver
ImportError: cannot import name 'stats_exporter' from 'opencensus.ext.stackdriver'

在比较Git存储库和本地venv/lib/python3.7/site-packages/...时,似乎pip版本与Github不兼容,因此我尝试通过克隆并使用setup.py

来安装它。
pip install ../opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz

这给了我以下错误:

(venv) Yehoshaphats-MacBook-Pro:present-value yehoshaphatschellekens$ pip install ../opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz 
Processing /Users/yehoshaphatschellekens/opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/s2/y6vcdc1105s8xlpb12slr9z00000gn/T/pip-req-build-7m1ibdpd/setup.py", line 17, in <module>
        from version import __version__
    ModuleNotFoundError: No module named 'version'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/s2/y6vcdc1105s8xlpb12slr9z00000gn/T/pip-req-build-7m1ibdpd/

类似的错误类型表明我需要升级setuptools,并尝试了:(

This post建议这可能与我使用的是python3有关,尽管我确实需要在我的python3 venv上安装此软件包,但version无法完成。

任何有关此问题的帮助都将很棒!

1 个答案:

答案 0 :(得分:1)

尝试一下:

#!/usr/bin/env python

import os

from opencensus.common.transports.async_ import AsyncTransport

from opencensus.ext.stackdriver import trace_exporter as stackdriver_exporter

from opencensus.trace import tracer as tracer_module
from opencensus.stats import stats as stats_module


def main():
    sde = stackdriver_exporter.StackdriverExporter(
                project_id=os.environ.get("PROJECT_ID"),
                transport=AsyncTransport)

    tracer = tracer_module.Tracer(exporter=sde)
    with tracer.span(name='doingWork') as span:
        for i in range(10):
            continue


if __name__ == "__main__":
    main()

grpcio==1.19.0
opencensus==0.3.1
opencensus-ext-stackdriver==0.1.1

NB OpenCensus库也需要gRPC。

您将需要:

  • 一个GCP项目及其项目ID(${PROJECT_ID}
  • 具有roles/cloudtrace.agent及其密钥的服务帐户。

然后:

virtualenv venv
source venv/bin/activate

export PROJECT_ID=[[YOUR-PROJECT-ID]]
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/key.json

pip3 install --requirement requirements.txt
python3 stackdriver.py