Skip to main content
 首页 » 编程设计

Python 模块对象在 OSX Sierra 上没有带有自制软件的 struct_time 属性,但在 debian 上没有

2025年02月15日8xiaohuochai

我有一个最奇怪的问题。此 Python 代码适用于运行股票 Debian 的 Raspberry Pi:

from datetime import datetime, date, time 
now = datetime.now() 
print( now ) 
print( now.strftime( "%d/%m/%y %H:%M" ) ) 

并给出预期的:

2017-07-17 08:17:17.567342 
17/07/17 08:17 

在通过 Homebrew 安装 Python 的 OSX Sierra 上,它给出:

2017-07-17 08:10:21.196849 
2017-07-17 08:10:21.197579 
Traceback (most recent call last): 
  File "time.py", line 6, in <module> 
    print( now.strftime( "%d/%m/%y %H:%M" ) ) 
  File "/Users/craign/Desktop/time.py", line 6, in <module> 
    print( now.strftime( "%d/%m/%y %H:%M" ) ) 
AttributeError: 'module' object has no attribute 'struct_time' 

我有:

$ brew --version 
Homebrew 1.2.4 
$ python --version 
Python 2.7.13 

有人有什么想法吗?我在互联网、StackOverflow 等网站上搜索了很长时间,感觉自己像个白痴。

请您参考如下方法:

您应该将脚本的名称 time.py 更改为 my_time.py 或您想到的任何内容,然后删除文件 time.pyc编译已经创建并且应该按预期工作。您的设置没有问题,这是预期的行为,可以轻松重现。

为什么?:模块 time 已经存在于 python 中,您错误地覆盖了它。