首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:“”module“”对象没有“”lowercase“”属性“”

AttributeError:“”module“”对象没有“”lowercase“”属性“”
EN

Stack Overflow用户
提问于 2013-12-13 03:30:09
回答 2查看 12.5K关注 0票数 8

我想我在导入pylab时遇到了麻烦。当我导入numpy时,也会发生类似的错误。以下是我的代码

代码语言:javascript
复制
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()

我得到了这个输出

代码语言:javascript
复制
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。

EN

回答 2

Stack Overflow用户

发布于 2020-02-29 21:16:47

2020附录:注意string.lowercase仅适用于Python2。

在Python3中,使用string.ascii_lowercase而不是string.lowercase

代码语言:javascript
复制
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'
票数 10
EN

Stack Overflow用户

发布于 2013-12-13 04:00:51

在评论中进行了一些讨论后,事实证明(就像通常内置模块突然提供AttributeErrors时一样),问题是另一个名为string的模块正在隐藏内置模块。

检查这一点的一种方法是查看模块的__file__属性,或者只查看repr本身:

代码语言:javascript
复制
>>> print string
<module 'string' from '/usr/lib/python2.7/string.pyc'>

如果from没有指向正确的位置,那么您就读取了错误的模块。

解决方案:删除/重命名有问题的string.py/string.pyc文件。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20552481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档