Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'module' object has no attribute 'lowercase'

I think I'm having troubles importing pylab. A similar error occurs when I import numpy. Here is my code

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()

I get this output

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'

Is there any problem with the syntax?

I'm using python2.7 on fedora18.

like image 746
orknaydn Avatar asked Dec 12 '13 19:12

orknaydn


2 Answers

2020 addendum: Note string.lowercase was for Python 2 only.

In Python 3, use string.ascii_lowercase instead of 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'
like image 65
Mark Avatar answered Nov 01 '22 09:11

Mark


After some discussion in the comments, it turned out (as it usually does when builtin modules suddenly seem to give AttributeErrors) that the problem was that another module named string was shadowing the builtin one.

One way to check this is to look at the __file__ attribute of the module, or just look at the repr itself:

>>> print string
<module 'string' from '/usr/lib/python2.7/string.pyc'>

if the from doesn't point to the right place, you've got the wrong module being read.

Solution: delete/rename the offending string.py/string.pyc files.

like image 21
DSM Avatar answered Nov 01 '22 08:11

DSM