Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Mtaplotlib AttributeError:&x27;模块';对象没有属性';pyplot';_Python 2.7_Matplotlib_Attributes - Fatal编程技术网

Python 2.7 Mtaplotlib AttributeError:&x27;模块';对象没有属性';pyplot';

Python 2.7 Mtaplotlib AttributeError:&x27;模块';对象没有属性';pyplot';,python-2.7,matplotlib,attributes,Python 2.7,Matplotlib,Attributes,在任何人说“重复问题”或其他任何问题之前,请继续阅读,这与其他问题不同 所以当我运行我的小程序时 python numbers.py 在哪里 我使用的是matplotlib版本1.2 /usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/ 我尝试了几种方法让它发挥作用 from matplotlib import pyplot as plt import matplotlib.pyplot

在任何人说“重复问题”或其他任何问题之前,请继续阅读,这与其他问题不同

所以当我运行我的小程序时

python numbers.py
在哪里

我使用的是matplotlib版本1.2

/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/
我尝试了几种方法让它发挥作用

from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
他们两个都不工作。它们都给出了相同的错误

$ python numbers.py
Traceback (most recent call last):
  File "numbers.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 26, in <module>
    from matplotlib.figure import Figure, figaspect
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 34, in <module>
    import matplotlib.colorbar as cbar
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/colorbar.py", line 29, in <module>
    import matplotlib.collections as collections
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/collections.py", line 23, in <module>
    import matplotlib.backend_bases as backend_bases
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/backend_bases.py", line 51, in <module>
    from PIL import Image
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 90, in <module>
    import numbers
  File "/home/will/Documents/python/numbers/numbers.py", line 1, in <module>
    import matplotlib.pyplot as plt
AttributeError: 'module' object has no attribute 'pyplot'

您正在对系统文件进行阴影处理并获得循环导入

请注意,回溯的第一个和最后一个文件正在尝试从同一个文件导入

将文件名更改为其他名称(并删除
pyc
文件),它应该可以工作


还可以查看哪一个有类似的问题。

我认为这可能是问题所在,但在更改后,它没有起作用。原来它只是导入了旧文件的.pyc文件,我没有删除它。
$ python numbers.py
Traceback (most recent call last):
  File "numbers.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 26, in <module>
    from matplotlib.figure import Figure, figaspect
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 34, in <module>
    import matplotlib.colorbar as cbar
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/colorbar.py", line 29, in <module>
    import matplotlib.collections as collections
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/collections.py", line 23, in <module>
    import matplotlib.backend_bases as backend_bases
  File "/usr/local/lib/python2.7/dist-packages/matplotlib-1.2.0-py2.7-linux-x86_64.egg/matplotlib/backend_bases.py", line 51, in <module>
    from PIL import Image
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 90, in <module>
    import numbers
  File "/home/will/Documents/python/numbers/numbers.py", line 1, in <module>
    import matplotlib.pyplot as plt
AttributeError: 'module' object has no attribute 'pyplot'
from matplotlib import pyplot as plt


def toString(number):
    if type(number) == float:
        return floatToString(number)
    elif type(number) == int:
        return intToString(number)
    elif type(number) == complex:
        return complexToString(number)
    else:
        return "NaN"


def intToString(number):
    if number < 0:
        return "minus " + intToStringPos(abs(number))
    else:
        return intToStringPos(number)

def intToStringPos(number):
    upTo20 = [""] + "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split()
    tenTimes = "twenty thirty fourty fifty sixty seventy eighty ninety hundred".split()
    logs = [""] + "thousand million billion trillion quadrillion".split()

    if len(str(number)) > 3:
        #split number into groups of three, from the right hand end    
        number = list(reversed(str(number)))

        groups = list(reversed([int("".join(reversed([number[i+j] for j in range(3) if i+j < len(number)]))) for i in range(0, len(number), 3)]))

        retString = ""
        for i, number in enumerate(reversed(groups)):
            if number == 0:
                continue
            elif i == 0 and len(str(number)) < 3:
                if number != 0:
                    retString = "and " + intToString(number)
            else:
                retString = intToString(number) + " " + logs[i] + " " + retString

        return retString.strip()

    elif 1000 > number > 99:
        tens = intToString(int(str(number)[1:]))
        if tens != "zero":
            return upTo20[number/100] + " hundred and " + tens
        else:
            return upTo20[number/100]

    elif 100 > number >= 20:
        return tenTimes[number/10-2] + " " + upTo20[number - 10*(number/10)]

    elif 0 < number < 20:
        return upTo20[number]

    elif number == 0:
        return "zero"

def floatToString(number):
    integerPart, decimalPart = divmod(number,1)
    retString = intToString(int(integerPart))
    if decimalPart > 0.0:
         return retString + " point " + " ".join([intToString(int(d)) for d in str(decimalPart)[2:]])
    else:
         return retString

def complexToString(number):
    retString = []
    if number.real > 0.0:
        retString.append(floatToString(number.real))
    if number.imag > 0.0:
        if number.imag == 1:
            retString.append("j")
        else:
            retString.append(floatToString(number.imag) + " j")

    return " plus ".join(retString)


lengths = []
for number in range(1000):
    print toString(number)      
    lengths.append(len(toString(number)))

print lengths

plt.plot(range(1000), lengths)
plt.pyplot.show()