Error when calling the metaclass bases: function() argument 1 must be code, not str Error when calling the metaclass bases: function() argument 1 must be code, not str python python

Error when calling the metaclass bases: function() argument 1 must be code, not str


You're getting that exception because, despite its class-like name, threading.Condition is a function, and you cannot subclass functions.

>>> type(threading.Condition)<type 'function'>

This not-very-helpful error message has been raised on the Python bugtracker, but it has been marked "won't fix."


Different problem than OP had, but you can also get this error if you try to subclass from a module instead of a class (e.g. you try to inherit My.Module instead of My.Module.Class). Kudos to this post for helping me figure this out.

TypeError: Error when calling the metaclass bases

For this one, the answer is that you probably named a python class the same thing as the module (i.e., the file) that it's in. You then imported the module and attempted to use it like a class. You did this because you, like me, were probably a Java programmer not that long ago :-). The way to fix it is to import the module.class instead of just the module. Or, for sanity's sake, change the name of the class or the module so that it's more obvious what's being imported.


With respect to subclassing a module, this is a really easy mistake to make if you have, for example, class Foo defined in a file Foo.py.

When you create a subclass of Foo in a different file, you might accidentally do the following (this is an attempt to subclass a module and will result in an error):

import Fooclass SubclassOfFoo(Foo):

when you really need to do either:

from Foo import Fooclass SubclassOfFoo(Foo):

or:

import Fooclass SubclassofFoo(Foo.Foo):