0

So I'm writing a program with

import datetime
import time

I'm using time to record the time it takes the program to run, and I need to check the date so if the file is more than a certain age, don't process it.

I keep getting this error when trying to use these two classes

Traceback (most recent call last):
  File "<stdin>", line 563, in <module>
  File "<stdin>", line 498, in main
AttributeError: type object 'datetime.time' has no attribute 'time'
shell returned 1

Is it not possible to use both time and datetime in one program?

Some of the code:

import PyPDF2
import re
import os
#Time testing
import time
#Using this to check if address is in proper format and to clean it up
import usaddress

#testing this one out
import datetime
from dateutil.parser import *
from dateutil.tz import *
from datetime import *

#Timer starts
start_time = time.time() #Error is referring to this line, line 498

#Opens 3 different files

#For file in folder parse it to text

#Writes some things to file

#Gets the date from the file

if date != None:
  fileDate = parse(date).year
  now = datetime.now()
  print now.year, now.month, now.day

#Ends the timer and prints the time to the console
print("--- %s seconds ---"  % round(time.time() - start_time, 2))
SPYBUG96
  • 1,089
  • 5
  • 20
  • 38
  • 2
    It cannot be reffering to that line. It is perfectly fine to use both. Just tried importing both and setting a `start_time` the way you do. You might have overwritten a method somewhere? – turnip Oct 24 '17 at 13:25
  • @Petar I'm pretty sure I didn't. Unless can another module overwrite a method? – SPYBUG96 Oct 24 '17 at 13:27
  • 2
    If you post a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) it will be much easier to debug. – DavidG Oct 24 '17 at 13:28
  • 1
    Do you have a file called `time.py`? That might be causing a problem – asongtoruin Oct 24 '17 at 13:28
  • @DavidG The rest of my code is just data manipulation from what I was able to get out of the file, then putting that data into CSV format, and writing to a bunch of files – SPYBUG96 Oct 24 '17 at 13:30
  • Somewhere you must be overwriting `time`. It looks like something along the lines of `time = datetime.time` which then produces the error you have when you try to do `time.time()` – turnip Oct 24 '17 at 13:32
  • 1
    There is another issue here. You are only importing datetime. datetime.now() will throw an error, you need to call datetime.datetime.now() or import datetime from datetime. – Martin Lear Oct 24 '17 at 13:32
  • **Don't** use `from datetime import *`, that is the cause of the problem – DavidG Oct 24 '17 at 13:35
  • Sorry everyone, I should have included all of my imports – SPYBUG96 Oct 24 '17 at 13:35
  • 2
    https://stackoverflow.com/questions/2386714/why-is-import-bad – DavidG Oct 24 '17 at 13:36
  • @DavidG Those are the only places in my code I touch datetime and time – SPYBUG96 Oct 24 '17 at 13:36

2 Answers2

4

Here is your problem:

import datetime
from dateutil.parser import *
from dateutil.tz import *
from datetime import *  # <<<< problems

First you are importing datetime and then you are importing everything from datetime.

Be explicit, and only import what you need.

from datetime import datetime

Then you can use it as datetime.now or whatever methods you may need.

As a rule of thumb, never import *. It causes exactly these sorts of issues.

turnip
  • 2,246
  • 5
  • 30
  • 58
0

The problems is:

from datetime import *

because it imports time from datetime. It's always better to import only what you need. But if you also really need this method you could do (for example):

from datetime import time as dt

That's why import * is dangerous...

Iago Díaz
  • 368
  • 1
  • 2
  • 10