module.__init__() takes at most 2 arguments error in Python

12,127

Solution 1

module.__init__() takes at most 2 arguments (3 given)

This means that you are trying to inherit from a module, not from a class. In fact, datasets.imdb is a module; datasets.imdb.imdb is your class.

You need to change your code so that it looks like this:

class imagenet(datasets.imdb.imdb):
    def __init__(self, image_set, devkit_path=None):
        datasets.imdb.imdb.__init__(self, image_set)

Solution 2

Here's another possible cause...

If you have an __init__.py file, make sure you import the super class before the derived ones.

Here's the WRONG way to do it:

from mymodule.InheritedA import InheritedA
from mymodule.InheritedB import InheritedB
from mymodule.Parent import Parent

The above will give an error:

TypeError: module.__init__() takes at most 2 arguments (3 given)

However this will work:

from mymodule.Parent import Parent
from mymodule.InheritedA import InheritedA
from mymodule.InheritedB import InheritedB

For example the file InheritedA.py might be:

from mymodule import Parent

class InheritedA(Agent):
    def __init__(self):
        pass

    def overridden_method(self):
        print('overridden!!')
Share:
12,127
London guy
Author by

London guy

Passionate about Machine Learning, Analytics, Information Extraction/Retrieval and Search.

Updated on July 12, 2022

Comments

  • London guy
    London guy almost 2 years

    I have 3 files, factory_imagenet.py, imdb.py and imagenet.py

    factory_imagenet.py has:

    import datasets.imagenet
    

    It also has a function call as

    datasets.imagenet.imagenet(split,devkit_path))
    ...
    

    imdb.py has:

    class imdb(object):
    def __init__(self, name):
        self._name = name
        ...
    

    imagenet.py has:

    import datasets
    import datasets.imagenet
    import datasets.imdb
    

    It also has

    class imagenet(datasets.imdb):
        def __init__(self, image_set, devkit_path=None):
            datasets.imdb.__init__(self, image_set)
    

    All three files are in the datasets folder.

    When I am running another script that interacts with these files, I get this error:

    Traceback (most recent call last):
      File "./tools/train_faster_rcnn_alt_opt.py", line 19, in <module>
        from datasets.factory_imagenet import get_imdb
      File "/mnt/data2/abhishek/py-faster-rcnn/tools/../lib/datasets/factory_imagenet.py", line 12, in <module>
        import datasets.imagenet
      File "/mnt/data2/abhishek/py-faster-rcnn/tools/../lib/datasets/imagenet.py", line 21, in <module>
        class imagenet(datasets.imdb):
    TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)
    

    What is the problem here and what is the intuitive explanation to how to solve such inheritance problems?