gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:45:21 27 4
gpt4 key购买 nike

这是我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com