Typeerror: module takes at most 2 arguments 3 given

Having Typeerror: module takes at most 2 arguments 3 given error when you are in the midst of coding?

Well, this error can be frustrating but this can lead you to fix what is the cause of the problem.

Technically this error implies that you are calling a module that has too many arguments however it only accepts specific arguments particularly two.

So, in this article, we will cover how this error occurs, determine its causes, and importantly, provide solutions to it.

What is Typeerror: module takes at most 2 arguments 3 given?

This error Typeerror: module takes at most 2 arguments 3 given indicates that a module is being called with too many arguments. Wherein a module can only accept two arguments, but it was given three.

Further, this could be due to a mistake in the code, where an extra argument was accidentally passed to the module.

Here is how the error occurs:

import math
result = math.pow(2, 3, 4)
print(result)

In this code, we’re trying to use the pow() function from the math module to raise the number 2 to the power of 3, modulo 4.

However, we’ve accidentally passed three arguments to the pow() function instead of two.

As a result, we get a TypeError because we’ve provided an extra argument which apparently, math.pow() function only takes two arguments (base and exponent).

Here is another example that triggered the Typeerror: module takes at most 2 arguments 3 given

import time
current_time = time.localtime(1624824000, True, "UTC")
print(current_time)

In this code, we’re trying to use the localtime() function from the time module to convert the Unix timestamp 1624824000 to local time.

However, we’ve accidentally passed three arguments to the localtime() function instead of two.

Obviously, we will get TypeError because we’ve provided an extra argument, since the time.localtime() function only takes two arguments (the Unix timestamp and an optional flag).

How to fix Typeerror: module takes at most 2 arguments 3 given

Since we know already how this error occurs, here are some solutions to fix this TypeError: module takes at most 2 arguments 3 given error.

Solution 1: Remove the extra argument

import math
result = math.pow(2, 3)
print(result)

In this solution, we’ve removed the third argument to math.pow(), so we’re only passing in the base and exponent.

The code now correctly raises 2 to the power of 3, and the expected output is 8.0.

Output:

8.0

Solution 2: Use a different function that accepts three arguments

import random
dice_roll = random.randrange(1, 7, 2)
print(dice_roll)

In this solution, we’ve replaced the random.randint() function with the random.randrange() function, which can accept three arguments (start, stop, and step).

Then we are generating a random integer between 1 and 6 with a step of 2 (so the possible outcomes are 1 or 3 or 5).

However, the expected output is a random integer between 1 and 6, but only the values 1, 3, or 5 are possible.

So here is the output:

1

Solution 3: Correctly use the function with three arguments

import time
current_time = time.localtime(1624824000)
print(current_time)

In this solution, we’ve corrected the use of time.localtime() by only passing in the Unix timestamp as the first argument.

And here we are not using the optional flag argument.

The expected output is a time.struct_time object representing the local time corresponding to the Unix timestamp 1624824000.

Output:

time.struct_time(tm_year=2021, tm_mon=6, tm_mday=28, tm_hour=4, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=179, tm_isdst=0)

Common reason why this error occur

Here are some common reasons why you might see the TypeError: module takes at most 2 arguments 3 given error message:

  • Passing too many arguments to a function
  • Using the wrong function
  • Providing incorrect arguments
  • Using a different version of the module
  • Incorrect syntax

Additional Resources

Anyway here are some other fixed typeerror wherein you can consider trying when facing these errors:

Conclusion

To conclude, we knew that Typeerror: module takes at most 2 arguments 3 given is caused by using too many arguments to a function, or using the wrong function and providing incorrect arguments.

To fix this error mainly remove the extra argument or use a different function that accepts three arguments.

That’s all for this article. I hope you have learned and fixed the error you are encountering.

Thank you! 😊