“AttributeError: ‘module’ object has no attribute ‘blah’”

  • The obvious cause of this is that the settings.py doesn’t have the directory containing blah listed in INSTALLED_APPS.
  • A less obvious cause: you’ll also get this error if the directory doesn’t contain a file __init__.py.

35 Comments

  • John

    Reply Reply May 15, 2008

    Thanks! I would have never guessed that from the stack trace.

  • Branko Vukelic

    Reply Reply June 22, 2008

    I’m just having the same error, and either of those do not apply. I have the app listed in INSTALLED_APPS, and I have the __init__.py.

  • macdet

    Reply Reply July 13, 2008

    Yes, that was mine :( __init__.py

    Thx to google and you!

  • Gustavo Rahal

    Reply Reply January 5, 2009

    In my case the reason was dirt .pyc files hanging around. I cleaned all pyc files and it worked

    • mangesh

      Reply Reply March 24, 2014

      thanks….worked for me too…

  • lacking imsave()

    Reply Reply April 6, 2009

    I’m new to the whole python world, but im having this same type of problem:

    —————————————————————————
    AttributeError Traceback (most recent call last)

    /Users/lzkelley/Programs/Biophysics/Templates/nc_make_ffts.py in ()
    55 fft_image /= m
    56
    —> 57 misc.imsave(“fft”+str(num_images)+”.png”,fft_image)
    58
    59

    AttributeError: ‘module’ object has no attribute ‘imsave’
    WARNING: Failure executing file:
    ————————————————————–

    and when i try to import the method explicitly

    —————————————————————————
    ImportError Traceback (most recent call last)

    /Users/lzkelley/Programs/Biophysics/Templates/nc_make_ffts.py in ()
    3 import pylab
    4 from scipy import *
    —-> 5 from scipy.misc import imsave
    6 from pylab import *
    7 import os

    ImportError: cannot import name imsave
    WARNING: Failure executing file:
    Python 2.5.4 (r254:67916, Apr 6 2009, 15:48:49)
    ————————————————-
    any thoughts?

  • muslu

    Reply Reply April 29, 2009

    i added settings.py and i include __init__.py in the folder but still have problem

  • Anonymous

    Reply Reply June 22, 2009

    Thanks so much! __init__.py was my problem :)

  • snf

    Reply Reply August 9, 2009

    just a brief note

    if you use import to include a module, make sure you don’t have a file named just the same as the module you are trying to include in current directory

    it seems that python takes preference for local files over modules thus import fails

    a rookie observation

    cheers

    • Jordan

      Reply Reply December 12, 2012

      That is a great observation, thanks. That was exactly what was happening to me. Thanks again!!

      • mar

        Reply Reply December 30, 2012

        Thanks so much!
        That was exactly my problem.

    • Vincent Khougaz

      Reply Reply August 1, 2013

      This was my issue. Plebeian mistake.

    • Erin

      Reply Reply January 4, 2014

      Wow, that worked for me. Took me quite awhile to figure it out! Thank you so much.

  • Gnarlodious

    Reply Reply September 5, 2009

    Thanks for the hint. In my case the script was named “Cookie”, the same as the module imported. One symptom was that I was getting double printing in both Terminal and browser.

  • aksegaly

    Reply Reply October 24, 2009

    this is my error
    attributeerror ‘module’ object has no attribute system
    help…

  • aksegaly

    Reply Reply October 24, 2009

    this is my error
    attributeerror ‘module’ object has no attribute system
    help…

  • aksegaly

    Reply Reply October 24, 2009

    this is my error
    attributeerror ‘module’ object has no attribute system
    affer the compilation
    help me…

  • Pete

    Reply Reply January 15, 2010

    thanks. my problem was that i had two versions of a python package (numpy). i was trying to invoke a method that was only in the new one, however it found the old package first, so it couldn’t locate the new method.

    anyway, your post got me looking in the right direction :)

  • Dan

    Reply Reply August 14, 2010

    Anyone here know how I can pass a regexp this module?:

    import re

    def htc(m):
    return chr(int(m.group(1),16))

    def urldecode(url):
    rex=re.compile(‘%([0-9a-hA-H][0-9a-hA-H])’,re.M)
    return rex.sub(htc,url)

    if __name__ == ‘__main__’:
    print urldecode(‘adasasdasd%20asdasdasdas’)

    Using re.urldecode gives me the same “‘module’ object has no attribute” error but I’m new at this and don’t really know what I’m doing. The module seem to work fine I just can’t figure out how to pass a regexp to urldecode. Example:

    match=re.compile(‘flv_url=(.+?)amp;url_bigthumb’).findall(link)
    urls=re.urldecode(‘match’)

    or

    match=re.compile(‘flv_url=(.+?)amp;url_bigthumb’).findall(link)
    print urldecode(‘match’)

    Thanks for any help.

  • Anonymous

    Reply Reply April 5, 2011

    I am seeing the replies as below
    Thanks so much! __init__.py was my problem…

    Can anyone explain me the problem of __init__.py problem?

  • masood

    Reply Reply October 3, 2011

    Hi..

    how to call java method in python

    please help me :-(

  • 4F2E4A2E

    Reply Reply October 25, 2011

    im my case i had the _init_.py, but it had some content like this:

    import settings
    try:
    import ldap
    CAN_USE = True
    except:
    CAN_USE = False

    so i removed it and everything is fine now…

  • Lijo

    Reply Reply May 11, 2012

    I have this wierd kind of error.
    I couldn’t find an answer for this anywhere.

    AttributeError: ‘module’ object has no attribute ‘walk’

    This happened when I tried to execute this command.
    os.chdir(‘H:/Project/’)
    for subdir,dir,files in os.walk(‘H:/Project/’):
    #print subdir
    #print dir
    #print files

    for file in files:
    if file == file_name:

    any help is appreciated !

  • bams

    Reply Reply June 8, 2012

    well thanks snf.
    In my case i had a python file random.py and was trying to import random module. Just deleting the py file really helped.
    Thanks..

  • bams

    Reply Reply June 8, 2012

    also someone talked about double printing, i had been wondering since a while why that was happening.

  • hobs

    Reply Reply October 30, 2012

    For me the problem was old pyc’s all over the place in Mac OS X.

    To double-check a find command on the pyc’s:

    cd /usr/local/lib/python2.7
    find . -type f -name “*.pyc” -exec sudo echo {} \;

    Once you’re happy with the list of files, delete them with:

    /usr/local/lib/python2.7
    find . -type f -name “*.pyc” -exec rm -f {} \;

  • maverik

    Reply Reply December 6, 2012

    when i try to import pylab i have the following error
    Traceback (most recent call last):
    File “”, line 1, in
    File “/usr/lib/pymodules/python2.7/pylab.py”, line 1, in
    from matplotlib.pylab import *
    File “/usr/lib/pymodules/python2.7/matplotlib/pylab.py”, line 221, in
    from matplotlib import mpl # pulls in most modules
    File “/usr/lib/pymodules/python2.7/matplotlib/mpl.py”, line 3, in
    from matplotlib import axes
    File “/usr/lib/pymodules/python2.7/matplotlib/axes.py”, line 8539, in
    Subplot = subplot_class_factory()
    File “/usr/lib/pymodules/python2.7/matplotlib/axes.py”, line 8531, in subplot_class_factory
    new_class = new.classobj(“%sSubplot” % (axes_class.__name__),
    AttributeError: ‘module’ object has no attribute ‘classobj’

    but if i try to import from the directory /usr/lib/python2.7/ it works fine what can be the possible problem

  • Amir

    Reply Reply December 10, 2012

    Can any Body help me with this error.
    I was running OPAddon.py (Tor Network Analysis Tool) and I got the below error.

    Traceback (most recent call last):
    File “./op-addon.py”, line 1366, in
    startup(sys.argv)
    File “./op-addon.py”, line 1196, in startup
    GeoIPSupport.GeoIPRouter, True)
    File “./op-addon.py”, line 626, in __init__
    self.model = NetworkModel(self.routers)
    File “./op-addon.py”, line 379, in __init__
    self.graph = networkx.XGraph(name=”Tor Subnet”)
    AttributeError: ‘module’ object has no attribute ‘XGraph’

  • Brandon Bertelsen

    Reply Reply December 23, 2012

    Another possibility is that you’ve called:

    def __unicode__(self):
    return self.name

    At the end of your model unnecessarily.

  • zi

    Reply Reply April 25, 2013

    I have this error in my python script for abaqus:
    attribute error:’constrained sketch’ object has no attribute ‘line’

    import sketch
    import part

    import connectorBehavior

    myModel = mdb.Model(name=’Model GNP-3D’)
    RVESketch = myModel.ConstrainedSketch(name=’Sketch RVE’, sheetSize=200.0)
    RVESketch.rectangle((0,0),(RVE_size[0],RVE_size[1]))
    RVE = myModel.Part(name=’RVE’, dimensionality=THREE_D, type=DEFORMABLE_BODY)
    RVE.BaseSolidExtrude(sketch=RVESketch, depth=RVE_size[2])
    for i in range(NUM_GNP):
    GNPname=’GNP-’+str(i)
    GNPSketch = myModel.ConstrainedSketch(name=GNPname, sheetSize=200.0)
    GNPSketch.ConstructionLine(point1=(0.0,-100.0), point2=(0.0, 100.0))
    GNPSketch.FixedConstraint(entity = GNPSketch.geometry[2])
    GNPSketch.EllipseByCenterPerimeter(axisPoint1=(0.0, a), axisPoint2=(b, 0.0), center=(0.0, 0.0))
    GNPSketch.CoincidentConstraint(addUndoState=False, entity1=GNPSketch.vertices[0], entity2=GNPSketch.geometry[2])
    GNPSketch.CoincidentConstraint(addUndoState=False, entity1=GNPSketch.vertices[2], entity2=GNPSketch.geometry[2])
    GNPSketch.line(point1=(0.0,50),point2=(0.0,-50))
    GNPSketch.VerticalConstraint(addUndoState=False, entity=GNPSketch.geometry[5])
    GNPSketch.ParallelConstraint(addUndoState=False, entity1=GNPSketch.geometry[2],entity2=GNPSketch.geometry[5])
    GNPSketch.CoincidentConstraint(addUndoState=False, entity1= GNPSketch.vertices[3], entity2=GNPSketch.geometry[2])
    GNPSketch.autoTrimCurve(curve1=GNPSketch.geometry[3], point1=(-b,0.0))
    GNP=myModel.Part(name=GNPname, dimensionality=THREE_D, type=DEFORMABLE_BODY)
    GNP.BaseSolidRevolve(angle=360,flipRevolveDirection=OFF, sketch=GNPSketch)

  • Ravi

    Reply Reply May 1, 2013

    You all need to check your python version, subprocess check_output issue has been fixed into python 2.7 +

  • mylanium

    Reply Reply August 12, 2013

    There are a lot of different reasons,
    and none among mentioned is mine (cyclic import). So to solve the problem, I’ve ran ./manage.py shell –trace
    The –trace option let you see the traceback and catch the reason of the problem!

  • Xavante

    Reply Reply January 19, 2014

    Thank you for this thread!! I got so frustrated with this problem that I reinstalled my OS twice to try fix it…

    Turns out it was a simple matter of renaming the modules with the same names in the base directory :( :(

    Thanks again – lifesaver @RachelWillmer, and @snf

  • Brian B

    Reply Reply February 23, 2014

    My problem was having a script named “io.py” in my project directory. After renaming this file, it worked. My error was:

    AttributeError: ‘module’ object has no attribute ‘BufferedIOBase’

  • Braulio

    Reply Reply February 28, 2014

    Another cause

    When you don’t write a module name correctly.

Leave A Response

* Denotes Required Field