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

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.

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'

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.

Related

Couldn't Run the .py file | AttributeError: module 'collections' has no attribute 'MutableSequence'

I am working with odx files and I have a generate.py file to run. I am using pyXB. When I try to run I am getting this.
*Traceback (most recent call last):
File "C:\Users\rohitkr\Downloads\starter_kit_adas-master\starter_kit_adas-master\devops\scripts\generate_odxf\generate_odxf.py", line 15, in
from schema import odx
File "C:\Users\rohitkr\Downloads\starter_kit_adas-master\starter_kit_adas-master\devops\scripts\generate_odxf\schema\odx.py", line 9, in import pyxb.binding
File "C:\Users\rohitkr\AppData\Local\Programs\Python\Python310\lib\site-packages\pyxb\binding_init_.py", line 8, in
from . import datatypes
File "C:\Users\rohitkr\AppData\Local\Programs\Python\Python310\lib\site-packages\pyxb\binding\datatypes.py", line 1266, in
rom . import content
File "C:\Users\rohitkr\AppData\Local\Programs\Python\Python310\lib\site-packages\pyxb\binding\content.py", line 807, in
class _PluralBinding (collections.MutableSequence):
AttributeError: module 'collections' has no attribute 'MutableSequence'*
'''
What could be the problem?
Thanks in advance.
In python 3.10 MutableSequence was removed from collections in favor of collections.abc
Deprecated since version 3.3, will be removed in version 3.10: Moved Collections Abstract Base Classes to the collections.abc module. For backwards compatibility, they continue to be visible in this module through Python 3.9.
>>> from collections import MutableSequence
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
ImportError: cannot import name 'MutableSequence' from 'collections' (C:\Program Files\Python310\lib\collections\__init__.py)
>>> from collections.abc import MutableSequence
If you don't want to change the source code, there is an easier way. Just use this in your script after importing.
import collections
collections.MutableSequence = collections.abc.MutableSequence

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

I'm using MacOs with the PyCharm environment, and my project is set to use Python 2.7.10. Now, I have on the first line of my module the line:
import numpy as np
When I try to run my code, I get the following error:
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /{path to file}/numpy_exercise.py
Traceback (most recent call last):
File "/{path to file}/numpy_exercise.py", line 1, in module>
import numpy as np
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/__init__.py", line 169, in module>
from . import polynomial
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/polynomial/__init__.py", line 20, in module>
from .polynomial import Polynomial
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/polynomial/polynomial.py", line 68, in module>
from .polytemplate import polytemplate
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/polynomial/polytemplate.py", line 17, in module>
polytemplate = string.Template(''' AttributeError: 'module' object has no attribute 'Template'
(note that "module>" was actually in brackets from both sides but because it gets omitted that way I daleted the first bracket so you can see it)
Though in Python 3 it does not happens to me. I looked on the web but couldn't find anything. I have no clue of why this is happening. I'd be glad to get some of your help!
Thank you guys!

Python module import difference "string.maketrans"

I have come across a strange python module import issue.
When I trying to import the boilerpipe module,
from boilerpipe.extract import Extractor
I got this exception:
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/boilerpipe-1.2.0-py2.7.egg/boilerpipe/extract/ __init__.py", line 2, in <module>
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 1140, in <module>
import ssl
File "/usr/lib/python2.7/ssl.py", line 58, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/usr/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
I've searched over internet and saying that in Python 2.6 the 'str' module has been renamed to 'string' module. So this looks like some where in code library it didn't import "string" module properly.
Yet the really strange thing is, when I run the python code from home directory and run the same piece of code (either by using python shell or using python pyfile.py), it works fine! No more import error.
So I'm bit confusing. Can anyone give me any hint?
Thanks!
Some other script in sys.path is called "string.py" and is masking the stdlib module.
Double check to make sure that you don't have a file string.py that has been imported.
To debug this, put somewhere:
import sys
raise Exception("string module: %r" %(sys.modules.get("string"), ))
That will tell you what string module was imported (or if it shows None, no string module has been imported yet).

Issue while importing nltk package in Python

When I type
import nltk
in the Python interpreter, it gives me this --
>>> import nltk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 105, in <module>
from collocations import *
File "/usr/local/lib/python2.7/dist-packages/nltk/collocations.py", line 38, in <module>
from nltk.metrics import ContingencyMeasures, BigramAssocMeasures, TrigramAssocMeasures
File "/usr/local/lib/python2.7/dist-packages/nltk/metrics/__init__.py", line 16, in <module>
from nltk.metrics.scores import (accuracy, precision, recall, f_measure,
File "/usr/local/lib/python2.7/dist-packages/nltk/metrics/scores.py", line 16, in <module>
from scipy.stats.stats import betai
File "/usr/lib/python2.7/dist-packages/scipy/stats/__init__.py", line 7, in <module>
from stats import *
File "/usr/lib/python2.7/dist-packages/scipy/stats/stats.py", line 198, in <module>
import distributions
File "/usr/lib/python2.7/dist-packages/scipy/stats/distributions.py", line 87, in <module>
from new import instancemethod
File "new.py", line 3
^
IndentationError: expected an indented block
I took a look at new.py which I found in /usr/lib/python2.7/ and found everything ok.
"""Create new objects of various types.
Deprecated.This module is no longer required except for backward compatibility.
Objects of most types can now be created by calling the type object.
"""
from warnings import warnpy3k
warnpy3k("The 'new' module has been removed in Python 3.0; use the 'types' "
"module instead.", stacklevel=2)
del warnpy3k
from types import ClassType as classobj
from types import FunctionType as function
from types import InstanceType as instance
from types import MethodType as instancemethod
from types import ModuleType as module
from types import CodeType as code
Any solutions?
You have a local file named new.py. Check your current directory and rename it or delete it.
You can see this in the traceback:
File "/usr/lib/python2.7/dist-packages/scipy/stats/distributions.py", line 87, in <module>
from new import instancemethod
File "new.py", line 3
The preceding module has a full filepath, but the new.py file does not, making it a local file that is shadowing the relative import in scipy.stats.distributions.

matplotlib basic question

Please forgive my simple question. I have just started to use Matplotlib and I am having some difficulty.
I can run the following with the interpretor without problems:
>>> from pylab import *
>>> plot([1,2,3])
>>> show()
The above code generates a beautiful graph.
However, if I place the following code inside a file and run it, I get an error:
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
from pylab import *
plot([1,2,3])
show()
Here is the error message:
Traceback (most recent call last):
File "/Users/sbrown/Desktop/new1.py", line 12, in <module>
from pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py", line 133, in <module>
from matplotlib.rcsetup import (defaultParams,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/rcsetup.py", line 19, in <module>
from matplotlib.colors import is_color_like
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/colors.py", line 54, in <module>
import matplotlib.cbook as cbook
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/cbook.py", line 15, in <module>
import new
File "/Users/sbrown/Desktop/new.py", line 2, in <module>
plot([1,2,3])
NameError: name 'plot' is not defined
>>>
Any idea what the problem could be? Thanks in advance for any help you can provide!
Looks like you have a file on your Desktop that is shadowing the standard Python new module:
>>> import new
>>> new
<module 'new' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/new.pyc'>
Rename or remove $HOME/Desktop/new.py and try again.

Categories