添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
vikas@server:~/memory_profiler-0.36$ ./mprof run --python test_sl.py 
vikas@server:~/memory_profiler-0.36$ ls *.dat
mprofile_20151001035123.dat

但当我试图绘制图形时,它却说 "绘制需要matplotlib"。

vikas@server:~/memory_profiler-0.36$ ./mprof plot --output=plot.png 
matplotlib is needed for plotting.

我错过了什么吗?

我正在分析的函数是run(),它在: file_to_be_profiled.py里面。

#!/usr/bin/python
import time
import os, sys, commands
from memory_profiler import profile
from guppy import hpy
@profile
def run():
    d = {}
    l = []
    hp = hpy()
    before = hp.heap()
    d["k1"] = 'val1'
    d["k2"] = 10
    count = 0
    while (count < 9):
        l.append(count)
        print 'The count is:', count
        count = count + 1
    print "Good bye!"
    after = hp.heap()
    leftover = after - before
    print leftover
if __name__ == '__main__':
    sys.exit(run())

而我是在我的测试脚本中调用run():test_sl.py。

#!/usr/bin/python
import commands
# for the time being I am running my test to profile method: run() inside file_to_be_profiled.py
run_cmd = './file_to_be_profiled.py
commands.getstatusoutput(run_cmd)

我在ubuntu机器上运行。但是apt-get安装对我来说并不奏效。然后我从源码安装。但是,我也得到了同样的错误。

apt-get失败。

vikas@server:~/memory_profiler-0.36$ sudo apt-get install -y python-matplotlib
[sudo] password for vikaskuk:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package python-matplotlib is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'python-matplotlib' has no installation candidate
vikask@server:~/memory_profiler-0.36$ ./mprof plot --output=plot.png
matplotlib is needed for plotting.

然后我试图进行管道安装......这也没有通过。

vikas@server:~/memory_profiler-0.36$ pip install matplotlib
Downloading/unpacking matplotlib
  Cannot fetch index base URL https://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement matplotlib
Cleaning up...
No distributions at all found for matplotlib
Storing debug log for failure in /home/vikaskuk/.pip/pip.log

最后我试着从源代码安装,似乎成功了。

vikas@server:~/memory_profiler-0.36$ cd ../matplotlib
vikas@server:~/matplotlib$ cd matplotlib-master/
(virtualenv)vikaskuk@beehive:~/matplotlib/matplotlib-master$ vi INSTALL
(virtualenv)vikaskuk@beehive:~/matplotlib/matplotlib-master$ python setup.py build
============================================================================
Edit setup.cfg to change the build options
BUILDING MATPLOTLIB
            matplotlib: yes [1.5.dev1]
                python: yes [2.6.6 (r266:84292, Sep 15 2010, 16:22:56)  [GCC
                        4.4.5]]
              platform: yes [linux2]
REQUIRED DEPENDENCIES AND EXTENSIONS
                 numpy: yes [not found. pip may install it below.]
                   six: yes [using six version 1.5.2]
              dateutil: yes [using dateutil version 2.1]
                  pytz: yes [pytz was not found. pip will attempt to install
                        it after matplotlib.]
               tornado: yes [tornado was not found. It is required for the
                        WebAgg backend. pip/easy_install may attempt to
                        install it after matplotlib.]
             pyparsing: yes [pyparsing was not found. It is required for
                        mathtext support. pip/easy_install may attempt to
                        install it after matplotlib.]
                libagg: yes [pkg-config information for 'libagg' could not
                        be found. Using local copy.]
              freetype: no  [The C/C++ header for freetype2 (ft2build.h)
                        could not be found.  You may need to install the
                        development package.]
                   png: no  [pkg-config information for 'libpng' could not
                        be found.]
                 qhull: yes [pkg-config information for 'qhull' could not be
                        found. Using local copy.]
OPTIONAL SUBPACKAGES
           sample_data: yes [installing]
              toolkits: yes [installing]
                 tests: yes [using nose version 1.3.0 / using mock 1.0.1]
        toolkits_tests: yes [using nose version 1.3.0 / using mock 1.0.1]
OPTIONAL BACKEND EXTENSIONS
                macosx: no  [Mac OS-X only]
                qt5agg: no  [PyQt5 not found]
                qt4agg: no  [PySide not found; PyQt4 not found]
               gtk3agg: no  [Requires pygobject to be installed.]
             gtk3cairo: no  [Requires cairocffi or pycairo to be installed.]
                gtkagg: no  [Requires pygtk]
                 tkagg: no  [The C/C++ header for Tk (tk.h) could not be
                        found.  You may need to install the development
                        package.]
                 wxagg: no  [requires wxPython]
                   gtk: no  [Requires pygtk]
                   agg: yes [installing]
                 cairo: no  [cairocffi or pycairo not found]
             windowing: no  [Microsoft Windows only]
OPTIONAL LATEX DEPENDENCIES
                dvipng: no
           ghostscript: yes [version 8.71]
                 latex: yes [version 3.1415926]
               pdftops: no
============================================================================
                        * The following required packages can not be built:
                        * freetype, png
(virtualenv)vikaskuk@beehive:~/matplotlib/matplotlib-master$ python setup.py install
============================================================================
Edit setup.cfg to change the build options
BUILDING MATPLOTLIB
            matplotlib: yes [1.5.dev1]
                python: yes [2.6.6 (r266:84292, Sep 15 2010, 16:22:56)  [GCC
                        4.4.5]]
              platform: yes [linux2]
REQUIRED DEPENDENCIES AND EXTENSIONS
                 numpy: yes [not found. pip may install it below.]
                   six: yes [using six version 1.5.2]
              dateutil: yes [using dateutil version 2.1]
                  pytz: yes [pytz was not found. pip will attempt to install
                        it after matplotlib.]
               tornado: yes [tornado was not found. It is required for the
                        WebAgg backend. pip/easy_install may attempt to
                        install it after matplotlib.]
             pyparsing: yes [pyparsing was not found. It is required for
                        mathtext support. pip/easy_install may attempt to
                        install it after matplotlib.]
                libagg: yes [pkg-config information for 'libagg' could not
                        be found. Using local copy.]
              freetype: no  [The C/C++ header for freetype2 (ft2build.h)
                        could not be found.  You may need to install the
                        development package.]
                   png: no  [pkg-config information for 'libpng' could not
                        be found.]
                 qhull: yes [pkg-config information for 'qhull' could not be
                        found. Using local copy.]
OPTIONAL SUBPACKAGES
           sample_data: yes [installing]
              toolkits: yes [installing]
                 tests: yes [using nose version 1.3.0 / using mock 1.0.1]
        toolkits_tests: yes [using nose version 1.3.0 / using mock 1.0.1]
OPTIONAL BACKEND EXTENSIONS
                macosx: no  [Mac OS-X only]
                qt5agg: no  [PyQt5 not found]
                qt4agg: no  [PySide not found; PyQt4 not found]
               gtk3agg: no  [Requires pygobject to be installed.]
             gtk3cairo: no  [Requires cairocffi or pycairo to be installed.]
                gtkagg: no  [Requires pygtk]
                 tkagg: no  [The C/C++ header for Tk (tk.h) could not be
                        found.  You may need to install the development
                        package.]
                 wxagg: no  [requires wxPython]
                   gtk: no  [Requires pygtk]
                   agg: yes [installing]
                 cairo: no  [cairocffi or pycairo not found]
             windowing: no  [Microsoft Windows only]
OPTIONAL LATEX DEPENDENCIES
                dvipng: no
           ghostscript: yes [version 8.71]
                 latex: yes [version 3.1415926]
               pdftops: no
============================================================================
                        * The following required packages can not be built:
                        * freetype, png

但当我试图绘制图表时,我又遇到了原来的问题。

vikas@server:~/matplotlib/matplotlib-master$ cd ../../memory_profiler-0.36
vikas@server:~/memory_profiler-0.36$ ./mprof plot --output=plot.png 
matplotlib is needed for plotting.

我能够成功地进行apt-get更新(有一些防火墙问题,然后我成功地进行:sudo apt-get install -y python-matplotlib。

但我在运行mprof时仍然遇到一些问题。

vikas@server:~/memory_profiler-0.36$ ./mprof plot --output=plot.png
Using last profile data.
Traceback (most recent call last):
  File "./mprof", line 494, in <module>
    actions[get_action()]()
  File "./mprof", line 454, in plot_action
    pl.figure(figsize=(14, 6), dpi=90)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 423, in figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

为什么我需要设置显示?我在ubuntu托管的机器上工作。

1 个评论
实际上,你从源码构建也没有成功。 你的机器上没有安装freetypelibpng,所以matplotlib无法构建。 替换代码2】遇到了一个单独的问题(你是在防火墙后面吗),但不久之后也会遇到同样的问题。 我不知道为什么你在通过软件包管理器安装时遇到问题,但是apt-get安装libfreetype6-devlibpng-devpython-tk,然后你应该可以从源码构建。
python
matplotlib
graph
memory-profiling
guppy
vikas
vikas
发布于 2015-10-01
4 个回答
Thomas Orozco
Thomas Orozco
发布于 2017-07-31
0 人赞同

你需要安装matplotlib,按照指示(mprof找不到matplotlib时,会抛出这个错误。).

你可以使用pip pip install matplotlib(你可能需要在前面加上sudo)或你的软件包管理器来完成。

  • sudo apt-get install -y python-matplotlib on Debian, Ubuntu and derivatives.
  • sudo yum install -y python-matplotlib on RHEL, CentOS and derivatives.
  • 我在ubuntu机器上运行。但是apt-get安装对我来说并不奏效。然后我从源代码安装。请编辑我所做的问题和步骤。
    @vikas 请确保你解释为什么apt-get install对你不起作用。是否有错误信息?
    我的组织中可能有某种防火墙......这就是为什么apt-get会失败。因此我尝试了源码安装,看起来确实成功了。但是,原来的问题也持续存在。请看我原来的问题....,我现在已经编辑了我所做的一切。
    @vikas 不,源码安装失败(见输出的结尾)。在你运行apt-get install之前,试着运行sudo apt-get update。你需要在某个时候让apt-get工作,因为你缺少matplotlib的很多依赖,而apt-get是现实中获得它们的唯一途径。
    在成功的apt-get更新之后,我能够安装matplotlib......但是mprof仍然抱怨显示问题。请看一下原始问题。我已经引用了我所得到的错误。
    Shang Yang
    Shang Yang
    发布于 2017-07-31
    0 人赞同

    似乎matplotlib:3.0.1 在 conda 环境中与 python 3.7 不兼容。 df.plot()命令无法正常执行,显示 "ImportError: matplotlib is required for plotting",但实际上matplotlib已经安装。

    我试着把matplotlib降级到3.0.0,然后就一切正常了。

    conda install -n [your_conda_envionment] matplotlib==3.0.0

    Andrey Pokhilko
    Andrey Pokhilko
    发布于 2017-07-31
    0 人赞同

    我在刚安装的Ubuntu 16.04上遇到了同样的问题。为了解决这个问题,我是这样安装SciPy的。 【替换代码0

    另外,我不得不像这样安装tkinter库。 sudo apt-get install python-tk

    我猜在过去的某个地方,matplotlib有scipy作为依赖项,但现在它只有numpy,而mprof plot需要scipy

    Vedanta6
    Vedanta6
    发布于 2017-07-31
    0 人赞同

    我有同样的问题。我使用memory_profiler lib键入以下命令 (mprof run runner.pymprof plot),得到一个信息 "matplotlib需要用于绘图"

    我试图按照我在这里找到的建议摆脱这个问题,但结果是我完成了这些简单的步骤,解决了这个问题。

  • 删除/usr/lib64/python2.7/Site-packages/中与matplotlib相关的文件(或者你可以用普通方法卸载matplotlib)。

  •