首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ImportError:没有名为“prometheus.client”的模块

ImportError:没有名为“prometheus.client”的模块
EN

Stack Overflow用户
提问于 2017-12-22 20:45:07
回答 4查看 12.3K关注 0票数 3

我目前正试图在互联网上运行一个样本代码 i代码。

在尝试使用python2.7运行它时,我得到以下错误:

代码语言:javascript
运行
复制
# python2.7 example.py 
Traceback (most recent call last):
  File "example.py", line 9, in <module>
    from http.server import HTTPServer
ImportError: No module named http.server

我试着运行以下命令:

代码语言:javascript
运行
复制
# pip install http.server
Collecting http.server
  Could not find a version that satisfies the requirement http.server (from versions: )
No matching distribution found for http.server

但是当我使用python3时,它不会给出错误。而是给出了错误:

代码语言:javascript
运行
复制
# python3 example.py 
Traceback (most recent call last):
  File "example.py", line 13, in <module>
    from prometheus.client import Gauge
ImportError: No module named 'prometheus.client'

我试着用pip和pip3安装prometheus.client,但什么也没有。

代码语言:javascript
运行
复制
# pip install prometheus.collectors
Collecting prometheus.collectors
  Could not find a version that satisfies the requirement prometheus.collectors (from versions: )
No matching distribution found for prometheus.collectors

# pip3 install prometheus.collectors
Collecting prometheus.collectors
  Could not find a version that satisfies the requirement prometheus.collectors (from versions: )
No matching distribution found for prometheus.collectors

我怎么才能得到丢失的图书馆?

编辑::

在下面的例子中,我使用了一种非常简约的方法(同样来自github)

代码语言:javascript
运行
复制
from prometheus_client import start_http_server, Summary
import random
import time

# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
    """A dummy function that takes some time."""
    time.sleep(t)

if __name__ == '__main__':
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        process_request(random.random())

运行上述代码将产生以下结果:

代码语言:javascript
运行
复制
# python3.4 main.yaml 
Traceback (most recent call last):
  File "main.yaml", line 1, in <module>
    from prometheus_client import start_http_server, Summary
ImportError: No module named 'prometheus_client'

而我确实安装了图书馆。

代码语言:javascript
运行
复制
# pip3 install prometheus_client
Requirement already satisfied: prometheus_client in /usr/lib/python2.7/site-packages
EN

回答 4

Stack Overflow用户

发布于 2019-03-19 21:16:37

我在ubuntu18.10上运行,我也遇到了同样的问题,而且我已经安装了python2。我可以通过安装python3和python3-pip来解决这个问题,如下所示。

代码语言:javascript
运行
复制
sudo apt install python3
sudo apt install python3-pip
pip3 install prometheus_client

可能对某人有帮助!

票数 3
EN

Stack Overflow用户

发布于 2017-12-22 20:49:03

http.server是Python3中的一个模块。

请参阅Python2中的SimpleHTTPServer注释:

注意,SimpleHTTPServer模块已经合并到Python3中的http.server中。2to3工具在将源转换为Python3时将自动调整导入。

对于prometheus.client,你想要pip install prometheus_client。请参阅python

您所链接的存储库中的README.md声明:

不推荐,使用:python

票数 1
EN

Stack Overflow用户

发布于 2020-01-02 21:28:21

我也面临着同样的问题。我使用pip3安装了python3 -m pip install --upgrade pip。之后运行了pip3 install prometheus_client。在这之后,问题就解决了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47947176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档