gpt4 book ai didi

Python 类型错误 'module' 对象不可调用

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:25 25 4
gpt4 key购买 nike

我是 python 的新手,所以这可能是我刚刚错过的东西......我只是想运行一个调用另一个文件的文件。

我有一个类似 myfile.py 的文件:

#!/usr/bin/python

import another_file

things = ... """ some code """

def mystuff(text, th=things):
return another_def(text, th)

'another_file' 可以自行编译/运行,并且有 def 'another_def' 和变量 'th'(这些是只是示例名称...)

所以我从命令行运行 python 然后尝试:

>>> import myfile
>>> t = myfile.mystuff('some text')

我得到了错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "myfile.py", line 18, in mystuff
return another_def(text, th)
TypeError: 'module' object is not callable

我尝试了 import another_file,即使它在 myfile.py 中,但这似乎没有任何区别。

如果有任何不同,我尝试过:

print myfile
<module 'myfile' from 'myfile.py'>
print myfile.mystuff
<function mystuff at 0x7fcf178d0320>

所以我假设如果它能找到文件和函数,问题是它如何尝试调用另一个文件……也许吧。任何帮助表示赞赏!

最佳答案

我不太确定为什么你得到一个TypeError(可能比你显示的更多),但如果你想访问来自 another_file 的函数,那么你应该这样做:

return another_file.another_def(text, th)

关于Python 类型错误 'module' 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19727049/

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