TypeError: 'module' object is not callable in my simple program about python module
Solution 1
You've imported the hello
module with the from fib import *
line, but you are not referencing the hello
function in that module.
Do this instead:
from fib import *
hello.hello()
or this:
from fib.hello import *
hello()
Solution 2
You are importing the module, not the method. You probably need to be doing hello.hello()
.
Solution 3
This is because you import module but not function in it, you can try:
hello.hello()
Solution 4
If you want to access hello()
after using only from fib import *
, you should replace your __init__.py
file with:
from hello import hello
from fib import fib
__all__ = ['fib', 'hello']
This imports the fib
and hello
functions into the top-level fib
module. This way, when you call from fib import *
, the function hello()
will be in your namespace, not the module hello
as you currently have it implemented.
thlgood
Updated on July 18, 2022Comments
-
thlgood almost 2 years
This is my Python module:
main.py fib/ __init__.py fib.py hello.py
fib.py
defined functionfib()
,hello.py
define functionhello()
.main.py
isfrom fib import * hello()
__init__.py
is__all__ = ["fib", "hello"]
I write this code just for practice.Not for work
I run
main.py
it print:Traceback (most recent call last): File "tes.py", line 5, in <module> hello() TypeError: 'module' object is not callable
Why? I had list hello in
__all__