我想我在导入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()我得到了这个输出
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'语法有什么问题吗?
我在fedora18上使用python2.7。
发布于 2020-02-29 21:16:47
2020附录:注意string.lowercase仅适用于Python2。
在Python3中,使用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'发布于 2013-12-13 04:00:51
在评论中进行了一些讨论后,事实证明(就像通常内置模块突然提供AttributeErrors时一样),问题是另一个名为string的模块正在隐藏内置模块。
检查这一点的一种方法是查看模块的__file__属性,或者只查看repr本身:
>>> print string
<module 'string' from '/usr/lib/python2.7/string.pyc'>如果from没有指向正确的位置,那么您就读取了错误的模块。
解决方案:删除/重命名有问题的string.py/string.pyc文件。
https://stackoverflow.com/questions/20552481
复制相似问题