26

I am creating a method in a class in a module mod1 and calling it as follows:

class blahblah:
   def foobar(self, bvar, **dvar)
       ////
       return dvar

And calling it as:

obj1 = mod1.blahblah()
dvar1 = obj1.foobar(True, **somedictionary)

It throws a Attribute error: blahblah has no attribute named foobar

Could you please help me with it? Thanks in advance

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
nerdy_darkknight
  • 615
  • 1
  • 8
  • 16
  • 2
    Clearly, you have not posted the code that you're actually using, for whatever reason. That's fine. But based on your post, there's nothing wrong. Are you sure that there's not a typo in your real code (perhaps you accidentally spelled `foobar` as `fubar`, etc)? – inspectorG4dget Oct 26 '12 at 21:35
  • No. I did check that and it wasnt typo for sure. Sorry didn't post the actual code because it is a security program. – nerdy_darkknight Oct 26 '12 at 21:37
  • 2
    This error can only occur if `foobar` is not a method defined inside `blahblah`. Since you assert that it is defined as such, you should not be seeing this error. Are you sure that `foobar` is defined inside `blahblah` in your real code? – inspectorG4dget Oct 26 '12 at 21:39
  • Then you need to post your real code. There's absolutely nothing wrong with the code you've posted… wait! what version of python are you using? – inspectorG4dget Oct 26 '12 at 21:41
  • 2
    Have you defined `blahblah` twice? Once with `foobar` defined, and once without? (i.e. overriding your previous definition) – Wilduck Oct 26 '12 at 21:41
  • 2
    @user1778309 please post the related excerpts from the real code. – Ashwini Chaudhary Oct 26 '12 at 21:41
  • 3
    Something you are assuming is true, is actually False. Take a look at `dir(mod1.blahblah)` (is `foobar` there?), `dir(obj1)` (is `foobar` there?), `obj1.__class__` (is it `mod1.blahblah`?), `obj1.__module__` (is it `mod1`?, etc. – unutbu Oct 26 '12 at 21:45
  • worked for me this way.
    from mod1 import blahblah
    from blah blah import foobar
    obj1 = foobar()
    Thanks for your support all
    – nerdy_darkknight Oct 26 '12 at 23:23
  • @inspectorG4dget i am using python2.7 – nerdy_darkknight Oct 26 '12 at 23:26
  • @user1778309: if that's what worked for you, then the code you posted does not represent the code you are working with at all; not in the least – inspectorG4dget Oct 26 '12 at 23:35

8 Answers8

40

The type of error you describe can be caused simply by mismatched indentation. If the method is at the very bottom of your class, move it up in the class a bit and the problem will become apparent.

When python interpreters run into mismatched indents (like say you started using tabs at the bottom of a file that was indented with spaces), the interpreter will not always throw an error; it can simply ignore the rest of the file. I ran into this just today while updating some old code where the original author used different whitespace chars (that happened to match my Geany tabs), and it threw me for a loop for a lot longer than I'd like to admit. :)

3vi1
  • 516
  • 6
  • 3
  • 1
    Thanks, this was helpful. I had the same issue where a mix of tabs and spaces were used for indentation in the file. I had read through a number of explanations without any resolution before finding your answer. – tompark Apr 19 '13 at 20:54
  • 6
    For what it's worth, it can also happen when using the %autoreload magic command in jupyter notebook, when you modify some methods in your module code (https://ipython.org/ipython-doc/3/config/extensions/autoreload.html#caveats) – Jacquot Jul 10 '17 at 13:51
  • This was helpful! Indentation was my issue as well. – Parvathy Sarat Apr 27 '20 at 23:57
6

When I ran into this problem, I immediately started checking for unbalanced indents, tabs, etc... Everything seemed correct but the error continued to appear. I walked away, came back, took another look, and DUH..., I found that I had a typo. instead of __init__(), I had typed __inti__(). So check all of your constructor's syntax first.

freeagh
  • 111
  • 2
  • 2
  • I spent two hours until read this comments and gave it a try... My method was called "def __int__(self):" and Pycharm didn't care. Thanks! – David Morabito May 26 '22 at 05:06
3

I had the same issue, and for me it happened when I moved the class file, but I left a .pyo file in the old folder, and python was still reading that .pyo file instead of reading the moved .py file.

maxgalbu
  • 432
  • 3
  • 16
3

Very old question, but I quote @Jacquot 's comment since it solved my problem (I was using %autoreload in ipython).

For what it's worth, it can also happen when using the %autoreload magic command in jupyter notebook, when you modify some methods in your module code (ipython.org/ipython-doc/3/config/extensions/…)

In particular, I solved the problem re-running the cell that was importing my class.

jcsun
  • 165
  • 9
2

Faced the same issue until I realized I had named the classes in both the files with the same name - pretty dumb!

2

Old question, but for those who are facing this problem and none of the other answers could help you, this could be helpful. I was using Pickle to save an entire class with some data inside it, and loading this class instance again, but I had added some class methods and attributes on init, that was why the interpreter couldn't find the new attributes described inside my class (It was loading the "old" class within the pickle object)

Ruan Kotovich
  • 579
  • 1
  • 4
  • 11
1

For Jupyter notebooks using VSCode, what worked for me is to restart VSCode after changes to the imported file.

Rowan Gontier
  • 821
  • 9
  • 14
0

In my case, I just added ClassName to the method call, and it started working:

Wrong:

import Clases.Class_filename as LWD
articles=LWD.method_name(parameters)

Corrected:

import Clases.ClassName as LWD
articles=LWD.ClassName.method_name(parameters)

And the Clases/Class_filename.py contains something like this:

class ClassName :
    def method_name(parameters):
     ....