在python 2.6.6中导入pylab时出错

时间:2016-10-31 22:44:04

标签: python numpy matplotlib redhat

我尝试在REHL 6.4上使用Python 2.6.6导入pylab

#!/usr/bin/env python
import os, zipfile,glob, shutil,time,scipy,numpy,matplotlib
from pylab import *

我收到错误:

# ./script.py
Traceback (most recent call last):
  File "./script.py", line 3, in <module>
    from pylab import *
  File "/usr/lib64/python2.6/site-packages/pylab.py", line 1, in <module>
    from matplotlib.pylab import *
  File "/usr/lib64/python2.6/site-packages/matplotlib/pylab.py", line 247, in <module>
    from matplotlib.pyplot import *
  File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 78, in <module>
    new_figure_manager, draw_if_interactive, show = pylab_setup()
  File "/usr/lib64/python2.6/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
    globals(),locals(),[backend_name])
  File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
    from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
  File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py", line 8, in <module>
    import gtk; gdk = gtk.gdk
  File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
    _init()
  File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
    _gtk.init_check()
RuntimeError: could not open display

已安装所有scipynumpymatplotlib,但我仍无法导入pylab。我导入pylab的方式有问题吗?

1 个答案:

答案 0 :(得分:0)

修复使用:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt