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

While importing modules in python one common error that you may face due to manual error is the TypeError: module() takes at most 2 arguments (3 given). Python programming language is known for having thousands of modules to perform various functions that can range from complex math which are part of the standard distribution, to all the way to data science and backend design. This in-depth article will help you resolve the scenarios in which this error may arise.

What is the “TypeError: module() takes at most 2 arguments (3 given)” Error?

The error message “TypeError: module() takes at most 2 arguments (3 given)” is a TypeError, which is one of the most common kinds of error in python. The “TypeError” indicates that there is a problem with the type of object being used or the number of arguments being provided. The error message occurs because you are trying to instantiate a module or a class, and you are providing more arguments than the constructor or initialiazer(usually __init__ method) can handle, as the latter part of the error message indicates.

What causes the “TypeError: module() takes at most 2 arguments (3 given)” Error?

Extra arguments

Let us illustrate the scenario where such an error can arise with an example:

class string_adder:

class string_adder:
    def __init__(self, inp_1):
        self.inp_1 = inp_1

    def add_string(self):
        return f"{self.inp_1} is great!"

my_string = string_adder('python', '3.10')
result_string = my_string.add_string()

print(result_string)    

The above code creates a class called string_adder, which takes a single user input called inp_1. We have initialized this class structure under the __init__ method. This is a special method in Python used for initializing object instances. It is called automatically when an object is created, setting its initial attributes after declaring the class structure. We write the function called add_string() to join the user input with another string. As you can see, the return function is supposed to give us the output in the format: “[user_input] is great!”

After escaping from the class block, we create an instance of the string_adder class called my_string that takes two arguments, ‘python’ and ‘3.10’. After that, you can call the add_string() method on the my_string instance and pass the result to a new variable result_string. Then, we print this variable using the print() function. Here is the output we get once we execute the code:

TypeError: Adder.__init__() takes 2 positional arguments, but 3 were given
TypeError: module() takes at most 2 arguments (3 given)

Let us return to the line where we first instantiated the string_adder class. Here, we see that we passed two arguments, ‘Python’ and ‘3.10’, which caused the error. This happened because when constructing our class string_adder, we coded it to accept only a user argument called inp_1.

Using a function that doesn’t accept three arguments

import random
roll = random.randint(1, 6, 1)
print(roll)

The above code snippet will give the following output:

TypeError: Random.randint() takes 3 positional arguments but 4 were given

The code given above will raise a variation of the “TypeError: module() takes at most 2 arguments (3 given)“. This is because the randint() function does not accept more than two arguments(start, stop). In this context, it is the incorrect function for the purpose. The solution to this problem is to use the correct function, which will be demonstrated in the solutions section.

Providing incorrect arguments

import time
time_now = time.localtime(1706372034, True, "UTC")
print(time_now)

In the above-demonstrated code, we will once again get the following error :

TypeError: localtime() takes at most 1 argument (3 given)

The above error is a variation of the TypeError: module() takes at most 2 arguments (3 given). We are using the localtime() function from the time module to convert the Unix timestamp provided as the argument to local time.

However, the code incorrectly passes three arguments to the function in place of two as determined earlier.

How do you resolve the “TypeError: module() takes at most 2 arguments (3 given)” Error?

Remove the third argument

We will solve the TypeError: module() takes at most 2 arguments (3 given) in the context of the code we used to demonstrate above. In line 8, Instead of passing two arguments, we will pass just one, resolving the error. Here is what the new code will look like:

class string_adder:
    def __init__(self, inp_1):
        self.inp_1 = inp_1

    def add_string(self):
        return f"{self.inp_1} is great!"

my_string = string_adder('python')
result_string = my_string.add_string()

print(result_string)

After doing this, this is the output we will get if we execute the code:

'Python is great.'

As stated before, the problem arose because we incorrectly passed two when the class only meant to take one. We fixed this by correctly passing only one argument as the class was meant to take. This was just an example demonstration of the error case. Your code may not be similar, but you are likely getting the error because you are passing the incorrect number of arguments to a module or class, more than it was constructed to take.

Use the correct function that takes three arguments

The code shown in case 2, which was giving us the error “TypeError: module() takes at most 2 arguments (3 given),” was happening due to the incorrect function being used. The correct function to use in that context is the random.randrange() function that accepts three arguments: start, stop, and a step argument. Below is the corrected code for the same:

import random
roll = random.randrange(1, 6, 2)
print(roll)

output –

1

A random integer between 1 and 5 is generated, along with that 2 numbers in between is skipped and is denoted by the 3rd argument in the function. So, the possible outcomes are 1, 3, or 5.

Provide the arguments in the correct order

We saw how the code in Case 3 will give us the error TypeError: module() takes at most 2 arguments (3 given). The TypeError was occurring due to arguments being passed to the function in the wrong order. The below code fixes the error:

import time
time_now = time.localtime(1706372034)
print(time_now)

In the solution code, We have correctly used the time.localtime() function by passing only the unix timestamp as the first and only argument. We are not using the optional flag arguments. The expected output should be –

time.struct_time(tm_year=2024, tm_mon=1, tm_mday=27, tm_hour=16, tm_min=13, tm_sec=54, tm_wday=5, tm_yday=27, tm_isdst=0)

It is a time.struct_time object, represents the local time corresponding to the unix timestamp passed to the argument.

FAQs

What is the self-attribute in all the Classes?

In Python classes, self is a conveniently used name for the first parameter of instance methods. It represents the object’s instance and allows access to its attributes and methods within the class.

Will providing fewer arguments than the class is meant to result in the same error?

You have to provide the exact number of arguments the Class should take. If you provide more, the error discussed in this article will occur. If you provide fewer arguments, you will also get a TypeError: module() missing 1 required positional argument.

Is there a limit to the number of parameters the __init__ method can take?

There is no strict limit to the number of arguments the __init__ method in Python can take. However, keeping the number of arguments reasonable for code readability and maintainability is generally advisable. As a best practice, consider using fewer parameters and form “focused classes” or grouping related parameters into meaningful data structures.

Conclusion

In this article, we learned about the scenario in which the error TypeError: module() takes at most 2 arguments (3 given) can arise. We deduced that this error arises due to three main reasons: incorrect number of arguments supplied to the Class/module, using an incorrect function, or writing the functions in the wrong order. The resolution to the cases of error, respectively, is to supply the correct number of arguments as the Class/module, using the correct function, or writing the arguments in the correct order.

TypeErrors are a common problem that occurs in this Python. In this instance, Class structures and methods are one of Python’s Object Oriented Programming strengths, which may be tricky for new coders who need to become more experienced with other OOP or low-level languages. They hence can run into errors such as the one illustrated above. But these errors can be resolved easily, usually by changing a single line of code.

References

  1. localtime()
  2. random.randrange()

That’s all for this post. To be up to date with our latest posts follow Python Clear. Let’s learn together!

Leave a Comment