gpt4 book ai didi

python - python中不能 'import time',得到 'AttributeError: struct_time'如何解决?

转载 作者:太空狗 更新时间:2023-10-30 02:14:07 25 4
gpt4 key购买 nike

在 Snow Leopard 上运行 python,我无法导入“时间”模块。在 ipython 中工作。没有加载任何 .pythonrc 文件。使用相同解释器“导入时间”的脚本运行良好。不知道如何解决这个问题。有人有想法吗?

[wiggles@bananas ~]$ python2.6
Python 2.6.6 (r266:84292, Sep 1 2010, 14:27:13)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "time.py", line 4, in <module>
t = now.strftime("%d-%m-%Y-%H-%M")
AttributeError: struct_time
>>>
[wiggles@bananas ~]$ ipython-2.6
Python 2.6.6 (r266:84292, Sep 1 2010, 14:27:13)
Type "copyright", "credits" or "license" for more information.

IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import time

In [2]:

最佳答案

查找名为 time.py 的文件。看起来 Python 正在导入它,而不是标准库中的那个:

  File "time.py", line 4, in <module>

解决方案是将文件重命名为“time.py”以外的名称。

顺便说一句,您可以通过打开 Python REPL 并键入来找到有问题的文件的路径。

In [1]: import time    
In [2]: time.__file__

In [3]: time     # This shows the path as part of the repr

关于python - python中不能 'import time',得到 'AttributeError: struct_time'如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4001211/

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