[英]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'
您定義的funvtion time()與導入的模塊時間具有相同的時間,因此會出現錯誤消息:'function'對象沒有屬性'strftime'
將您的函數重命名為Time(),它應該可以工作(我想)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.