Questions tagged [attributeerror]

Use this with questions involving the Python `AttributeError` exception. Use the more general "Python" tag as well when using this tag.

Explanation

In Python, an AttributeError is raised when:

... an attribute reference or assignment fails.

In other words, this happens when you try to access an attribute of an object or a class which does not exist.


Example

For example, trying to do:

import math
math.cuberoot(27)

raises an AttributeError similar to:

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    math.cuberoot(27)
AttributeError: 'module' object has no attribute 'cuberoot'

because the math module does not define a function called cuberoot.


Documentation

For more information, see the Python documentation on Attribute References.

2510 questions
2383
votes
16 answers

How to check if an object has an attribute?

How do I check if an object has some attribute? For example: >>> a = SomeClass() >>> a.property Traceback (most recent call last): File "", line 1, in AttributeError: SomeClass instance has no attribute 'property' How do I tell if…
Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83
449
votes
10 answers

Why do I get AttributeError: 'NoneType' object has no attribute 'something'?

I am getting an error message that says AttributeError: 'NoneType' object has no attribute 'something' How can I understand this message? What general scenarios might cause such an AttributeError, and how can I identify the problem? This is a…
Jacob Griffin
  • 4,807
  • 3
  • 16
  • 11
251
votes
20 answers

AttributeError: 'module' object has no attribute

I have two python modules: a.py import b def hello(): print "hello" print "a.py" print hello() print b.hi() b.py import a def hi(): print "hi" When I run a.py, I get: AttributeError: 'module' object has no attribute 'hi' What does the…
Stephen Hsu
  • 5,127
  • 7
  • 31
  • 39
219
votes
10 answers

Why do I get "'str' object has no attribute 'read'" when trying to use `json.load` on a string?

In Python I'm getting an error: Exception: (, AttributeError("'str' object has no attribute 'read'",), ) Given python code: def getEntries (self, sub): url =…
RobinJ
  • 5,022
  • 7
  • 32
  • 61
186
votes
22 answers

Why Python 3.6.1 throws AttributeError: module 'enum' has no attribute 'IntFlag'?

I just installed Python 3.6.1 for MacOS X When I attempt to run the Console(or run anything with Python3), this error is thrown: AttributeError: module 'enum' has no attribute 'IntFlag' $…
BryanWheelock
  • 12,146
  • 18
  • 64
  • 109
148
votes
8 answers

__getattr__ on a module

How can implement the equivalent of a __getattr__ on a class, on a module? Example When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
120
votes
16 answers

AttributeError: module 'time' has no attribute 'clock' in Python 3.8

I have written code to generate public and private keys. It works great at Python 3.7 but it fails in Python 3.8. I don't know how it fails in the latest version. Help me with some solutions. Here's the Code: from Crypto.PublicKey import RSA def…
user11576444
110
votes
9 answers

Why am I getting AttributeError: Object has no attribute?

I have a class MyThread. In that, I have a method sample. I am trying to run it from within the same object context. Please have a look at the code: class myThread (threading.Thread): def __init__(self, threadID, name, counter, redisOpsObj): …
Shades88
  • 7,934
  • 22
  • 88
  • 130
105
votes
3 answers

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

I am trying to write a program that will download mp3's off of a website then join them together but whenever I try to download the files I get this error: Traceback (most recent call last): File "/home/tesla/PycharmProjects/OldSpice/Voicemail.py",…
Sike1217
  • 1,065
  • 2
  • 8
  • 5
98
votes
9 answers

AttributeError: 'Tensor' object has no attribute 'numpy'

How can I fix this error I downloaded this code from GitHub. predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].numpy() throws the error AttributeError: 'Tensor' object has no attribute 'numpy' Please help me fix this! I…
Frieder Hannenheim
  • 1,144
  • 1
  • 7
  • 11
97
votes
4 answers

Error "'DataFrame' object has no attribute 'append'"

I am trying to append a dictionary to a DataFrame object, but I get the following error: AttributeError: 'DataFrame' object has no attribute 'append' As far as I know, DataFrame does have the method "append". Code snippet: df =…
Maksimjeet Chowdhary
  • 1,153
  • 1
  • 5
  • 11
85
votes
6 answers

AttributeError while querying: Neither 'InstrumentedAttribute' object nor 'Comparator' has an attribute

The following code: Base = declarative_base() engine = create_engine(r"sqlite:///" + r"d:\foo.db", listeners=[ForeignKeysListener()]) Session = sessionmaker(bind = engine) ses = Session() class Foo(Base): __tablename__ =…
Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
81
votes
4 answers

AttributeError: 'Series' object has no attribute 'reshape'

I'm using sci-kit learn linear regression algorithm. While scaling Y target feature with: Ys = scaler.fit_transform(Y) I got ValueError: Expected 2D array, got 1D array instead: After that I reshaped using: Ys =…
Hrvoje
  • 13,566
  • 7
  • 90
  • 104
71
votes
13 answers

AttributeError: module 'datetime' has no attribute 'now'

I am learning Python on my own. Now I have encountered some problems. Below is my code which copy from the video who running well. import datetime print(type(datetime)) d1 = datetime.datetime.now() print(d1) when I running the code using Pycharm &…
Yq Lee
  • 821
  • 1
  • 6
  • 4
69
votes
6 answers

"import datetime" v.s. "from datetime import datetime"

I have a script that needs to execute the following at different lines in the script: today_date = datetime.date.today() date_time = datetime.strp(date_time_string, '%Y-%m-%d %H:%M') In my import statements I have the following: from datetime…
codingknob
  • 11,108
  • 25
  • 89
  • 126
1
2 3
99 100