python - TypeError: 'module' object is not callable in my simple program about python 模块

标签 python typeerror python-import python-module

这是我的 Python 模块:

main.py
fib/
    __init__.py
    fib.py
    hello.py

fib.py 定义函数fib(), hello.py 定义函数hello().

main.py

from fib import *
hello()

__init__.py

__all__ = ["fib", "hello"]

我写这段代码只是为了练习,不是为了工作

我运行 main.py 它打印:

Traceback (most recent call last):
  File "tes.py", line 5, in <module>
    hello()
TypeError: 'module' object is not callable

为什么?我在 __all__

中列出了 hello

最佳答案

您已经使用 from fib import * 行导入了 hello 模块,但您没有引用该模块中的 hello 函数.

改为这样做:

from fib import *
hello.hello()

或者这个:

from fib.hello import *
hello()

关于python - TypeError: 'module' object is not callable in my simple program about python 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10705811/

相关文章:

python - 限制嵌入式 python 实例的功能

python - 导入仅在 Python 函数内部起作用

python - 如何在不导入 python 模块的情况下检查运行时是否有效?

javascript - WordPress 中的 jQuery slider 和未捕获的 TypeError

python - TypeError: __getitem__() takes exactly 2 arguments (2 given) TypeError? ( python 3)

python - 是否存在无法创建深拷贝的对象?

python - 如何从 Anaconda Tensorflow 环境启动 Spyder?

python - 读取文件时避免出现空格

python - 类型错误 : __init__() got an unexpected keyword argument 'check_hostname' in GAE with https

python - 将集合的元素附加到 Python 中的列表