在导入的模块中使用导入的库(Use imported library in imported module)
我试图通过另一个文件中的函数使用time.strftime()但由于某种原因导入time库不起作用(如果省略import time ,输出看起来完全相同):
foo.py:
import bar print bar.test() print bar.time()bar.py:
import time def test(): return "check!" def time(): return time.strftime("%H:%M:%S")输出:
import_test>python foo.py check! Traceback (most recent call last): File "foo.py", line 4, in <module> print bar.time() File "import_test\bar.py", line 7, in time return time.strftime("%H:%M:%S") AttributeError: 'function' object has no attribute 'strftime'I'm trying to use time.strftime() via a function from another file but for some reason the import of time library is not working (if I omit the import time the output looks exactly the same):
foo.py:
import bar print bar.test() print bar.time()bar.py:
import time def test(): return "check!" def time(): return time.strftime("%H:%M:%S")Output:
import_test>python foo.py check! Traceback (most recent call last): File "foo.py", line 4, in <module> print bar.time() File "import_test\bar.py", line 7, in time return time.strftime("%H:%M:%S") AttributeError: 'function' object has no attribute 'strftime'最满意答案
您定义的funvtion time()与导入的模块时间相同,因此出现错误消息:'function'对象没有属性'strftime'
将您的函数重命名为Time(),它应该可以工作(我猜)
You're defining funvtion time() that has the same time as imported module time, thus an error message: 'function' object has no attribute 'strftime'
Rename your function to Time() and it should work (I guess)
更多推荐
发布评论