AttributeError:'module'对象没有属性'lowercase'

ork*_*ydn 6 python import numpy matplotlib attributeerror

我想我在导入pylab时遇到了麻烦.导入numpy时会发生类似的错误.这是我的代码

from math import radians, sin, cos
from pylab import plot, xlabel, ylabel, title, show

v0=input("Enter v0 (m/s)...")
alpha0=input("enter alpha0 (degrees)...")
g=input("Enter g (m/s^2)..")

radalpha0=radians(alpha0)
t_inc=0.01
t=0
i=0
x=[]
y=[]

x.append(v0*cos(radalpha0)*t)
y.append(v0*sin(radalpha0)*t-0.5*g*t*t)

while y[i]>=0:
    i=i+1
    t=t+t_inc
    x.append(v0*cos(radalpha0)*t)
    y.append(v0*sin(radalpha0)*t-0.5*g*t*t)

xlabel('x')
ylabel('x')
plot(x,y)
title('Motion in two dimensions')
show()
Run Code Online (Sandbox Code Playgroud)

我得到了这个输出

Traceback (most recent call last):
  File "2d_motion.py", line 2, in <module>
    from pylab import plot, xlabel, ylabel, title, show
  File "/usr/lib64/python2.7/site-packages/pylab.py", line 1, in <module>
    from matplotlib.pylab import *
  File "/usr/lib64/python2.7/site-packages/matplotlib/__init__.py", line 151, in <module>
    from matplotlib.rcsetup import (defaultParams,
  File "/usr/lib64/python2.7/site-packages/matplotlib/rcsetup.py", line 19, in <module>
    from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
  File "/usr/lib64/python2.7/site-packages/matplotlib/fontconfig_pattern.py", line 28, in <module>
    from pyparsing import Literal, ZeroOrMore, \
  File "/usr/lib/python2.7/site-packages/pyparsing.py", line 109, in <module>
    alphas = string.lowercase + string.uppercase
AttributeError: 'module' object has no attribute 'lowercase'
Run Code Online (Sandbox Code Playgroud)

语法有问题吗?

我在fedora18上使用python2.7.

Mar*_*ark 7

2020 年附录:注意string.lowercase仅适用于 Python 2。

在 Python 3 中,使用string.ascii_lowercase代替string.lowercase

Python 3.6.8 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin
>>> import string
>>> string.lowercase
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'string' has no attribute 'lowercase'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
Run Code Online (Sandbox Code Playgroud)


DSM*_*DSM 6

在评论中进行了一些讨论之后,事实证明(正如内置模块突然似乎给出的那样AttributeErrors),问题在于另一个名为string阴影的内置模块.

检查这个的一种方法是查看__file__模块的属性,或者只看一下repr它自己:

>>> print string
<module 'string' from '/usr/lib/python2.7/string.pyc'>
Run Code Online (Sandbox Code Playgroud)

如果from没有指向正确的位置,则表示您正在读取错误的模块.

解决方案:删除/重命名违规string.py/ string.pyc文件.