在导入的模块中使用导入的库(Use imported library in imported module)

编程入门 行业动态 更新时间:2024-10-08 07:38:17
在导入的模块中使用导入的库(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)

更多推荐

本文发布于:2023-07-09 10:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   imported   module   library

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!