TypeError: ‘module’ object is not callable - Stack Overflow
Hey Apksha,
I encountered the same error message:
TypeError: ‘module’ object is not callable
when attempting to call time()
from the time
module:
import time
print(time()) # Here
To resolve this error, I called time.time()
instead:
import time
print(time.time()) # Here
Alternatively, I imported time
from time
:
from time import time # Here
print(time())
I also encountered the same error in a Django view when using @transaction
:
from django.http import HttpResponse
from django.db import transaction
# ↓ Here ↓
@transaction
def test(request):
return HttpResponse("Test")
To fix this, I replaced @transaction
with @transaction.atomic
:
from django.http import HttpResponse
from django.db import transaction
# ↓ Here ↓
@transaction.atomic
def test(request):
return HttpResponse("Test")
Hello Apksha,
When configuring a console_scripts
entry point in setup.py
, I encountered an issue when the entry point was a module or package rather than a function within the module.
The error message I received was:
Traceback (most recent call last):
File "/Users/ubuntu/.virtualenvs/virtualenv/bin/mycli", line 11, in <module>
load_entry_point('my-package', 'console_scripts', 'mycli')()
TypeError: 'module' object is not callable
For example, if I had the following setup:
from setuptools import setup
setup (
# ...
entry_points = {
'console_scripts': ['mycli=package.module.submodule']
},
# ...
)
I should have instead specified a callable function within the module:
from setuptools import setup
setup (
# ...
entry_points = {
'console_scripts': ['mycli=package.module.submodule:main']
},
# ...
)
This change ensures that the entry point refers to a callable function rather than the module itself. It’s important to note that adding an if __name__ == '__main__':
block to the module will not make the module callable in this context.
Hiii Apksha,
I encountered the same issue and found a solution by not using from YourClass import YourClass
. Instead, I copied the entire code from YourClass.py
and ran it directly in the main or current code. This approach resolved the error for me.