0

I have the following project structure:

- equalizer
    - __init__.py
    - equalizer.py
    - lms.py
- main.py

__init__.py is empty.

equalizer.py defines a base class named equalizer, like so:

class equalizer:
    var1 = []
    def __init__(self, data):
        self.var1 = data

lms.py defines a child class lms, which derives from equalizer:

from equalizer import equalizer

class lms(equalizer):
    def __init__(self, data):
        super().__init__(self, data)

    def lms(self):
        print('Hello World!')

Finally, in main.py, I instantiate the child:

import equalizer.lms as eq

if __name__ == "__main__":
    data = []

    lms_eq = eq.lms(data= data)
    lms_eq.lms()

When I run the code starting from main.py, I get this error:

Traceback (most recent call last):
  File "C:\Users\...\main.py", line 4, in <module>
    import equalizer.lms as eq
  File "C:\Users\...\equalizer\lms.py", line 6, in <module>
    class lms(equalizer):
TypeError: module() takes at most 2 arguments (3 given)

What is wrong, and how do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    `from equalizer import equalizer` is importing the *module* - equalizer.py in a folder named equalizer. The actual class is one level deeper - `from equalizer.equalizer import equalizer` perhaps. (And perhaps using the same name for everything isn't such a good idea...) – jasonharper Mar 13 '23 at 21:58
  • Maybe it's the matter of this example, but it's hard to read. Classes should have names starting with capital letters, there shouldn't be in general a method called the same as the class... – Gameplay Mar 13 '23 at 21:58
  • 1
    please don't name packages/modules/classes all the same, it's terribly confusing for no benefit. – wim Mar 13 '23 at 22:01
  • I added an answer to the canonical for the error message, that should also clear up everything that's specific to your circumstances. – Karl Knechtel Mar 13 '23 at 23:40

1 Answers1

0

Make your life easier and have python class names as camel caps to distinguish them from the module.

The issue is right here:

from equalizer import equalizer

You didn't relative import the module, so this is more like:

from <package equalizer> import <equalizer.py>

You want:

from .equalizer import equalizer

Which makes it more akin to:

from <equalizer.py> import <class equalizer>
flakes
  • 21,558
  • 8
  • 41
  • 88