Dribbler Dribbler - 2 months ago 10
Python Question

Python module object has no attribute struct_time with homebrew on OSX Sierra but not debian

I have the strangest problem. This Python code works on a Raspberry Pi running stock Debian:

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

And gives the expected:

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

On OSX Sierra with Python installed via Homebrew it gives:

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'

I have:

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

Anybody have any ideas? I've scoured the interwebs, StackOverflow, etc. for a crazy amount of time on this and feel like a total idiot.

Answer Source

You should change your script's name time.py by my_time.py or whatever pops in your mind and remove the file time.pyc that the compilation has created and it should work as expected. Nothing wrong with your setup, this is the expected behaviour and it can be easily reproduce.

Why?: The module time already exists in python and you overrode it by mistake.