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

时间:2017-07-17 13:27:12

标签: python macos

我有一个最奇怪的问题。这个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等等,感觉就像一个白痴。

1 个答案:

答案 0 :(得分:1)

您应该通过time.py更改脚本的名称my_time.py或者您想到的任何弹出窗口,并删除编译已创建的文件time.pyc,它应该按预期工作。您的设置没有任何问题,这是预期的行为,可以很容易地重现。

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