Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Yet Another AttributeError: 'module' object has no attribute

I tried reading through some of the other questions but I still can't get it to work.

Basically I'm using a quick and dirty function called time_string() to return the date and time in a string formatted the way I want. If I run time_string directly, it works fine. If I call it from another function I get an AttributeError.

time_string

import time
def time_string(): #Never mind the unreadable formatting
    return str(time.localtime().tm_hour)+':'+str(time.localtime().tm_min)+':'+str(time.localtime().tm_sec)+\
           ' '+str(time.localtime().tm_year)+'/'+str(time.localtime().tm_mon)+'/'+str(time.localtime().tm_mday)

if __name__ == '__main__':
    print time_string()

Running time_string directly

13:46:13 2012/7/19

Other function

from misc.time_string import time_string
def main():
    print time_string()

if __name__ == '__main__':
    main()

Running other function

Traceback (most recent call last): File "#Filepath#", line 10, in main() File "#Filepath#", line 7, in main print time_string() File "#Filepath#", line 9, in time_string ' '+str(time.localtime().tm_year)+'/'+str(time.localtime().tm_mon)+'/'+str(time.localtime().tm_mday) AttributeError: 'module' object has no attribute 'localtime'

I'm assuming its some issue with time not getting imported or something but it's boggling my mind

Thanks for the help!

like image 439
Scart Avatar asked Dec 05 '22 15:12

Scart


1 Answers

The problem is that you have or at one time had a time.py file in the directory where you run the script, causing the wrong time module to be imported.

Even if you remove the time.py file, there still is a compiled time.pyc file that gets imported.

like image 64
Otto Allmendinger Avatar answered Dec 07 '22 03:12

Otto Allmendinger