Confusion on proper use of referencing IMPORT methods

Hi. I’m relatively new to Python but am just completing a 600+ line program Weather/WebCam program. One reason it’s large is because since it will run attended for large periods of time, I’m trying to catch errors, restart or reboot.

My question is around proper reference to methods/functions in date time.

Here is some code I found/adjusted for my use. Works fine standalone.

def waitToTomorrow():

    tomorrow = datetime.datetime.replace(datetime.datetime.now() + datetime.timedelta(days=1),
                   hour=0,minute=0,second=0)
    delta = tomorrow - datetime.datetime.now()
    print("Sleeping "+str(delta.seconds))
    time.sleep(delta.seconds)

However, when I move the code into my program, Python complains that datetime is not an attribute. However, if add the IMPORT statement from the example, and use the dt reference, Python is happy.

Here is what I have to use

def wait_till_tomorrow():

    tomorrow = dt.datetime.replace(dt.datetime.now() + dt.timedelta(days=1),
                   hour=0,minute=0,second=0)
    delta = tomorrow - dt.datetime.now()
    if debug:
        logging.debug("Skipping waiting for tomorrow")
    elif reboot:
        pass
    else:
        time.sleep(delta.seconds+120) # Force past midnight wait
        if verbose: info.logging("Waiting till midnight.""{:3.2f}".format(delta.seconds/60)+" mins")

    return

So, my imports for datetime look like this because I’m having problems sorting everthing out.

import datetime
import datetime as dt
from datetime import datetime
from datetime import timedelta

So, looking for insights of what I am doing wrong.

Thanks

Hi. I’m relatively new to Python but am just completing a 600+ line
program Weather/WebCam program. One reason it’s large is because since
it will run attended for large periods of time, I’m trying to catch
errors, restart or reboot.

My question is around proper reference to methods/functions in date time.
[…]
import datetime
import datetime as dt
from datetime import datetime
from datetime import timedelta

The datetime module is a little unfortunate in that the “datetime” class
is spelt the same way.

You attribute issue is likely because you imported the class name, and
your datetime.datetime.now() call assumes you imports the module.

My personal habit is to never import the module, just the names inside:

from datetimeimport date, datetime, timezone

(Adjust according to what you’re using of course.)

That way you’re always talking about the class and never the module and
there is no confusion.

BTW, I usually import modules as their primary name if I do import a
module (never datetime because of the unfortunate naming). And I
particularly don’t:

import datetime as dt

because I use dt as a variable name for ad hoc datetime instances.
Maybe that’s just me.

It is common to import the numpy module as np, as a counter
example.

Cheers,
Cameron Simpson cs@cskk.id.au

Thanks for the tip @cameron. I’ll try that. At the moment, the program is running in dry run.

I think I need to pay attention to which functions I’m importing so I can avoid this.

Most of my imports are the:

from blah import name1, name2

form. But not all, and some of it comes down to personal taste. But
consistency in whatever habit you adopt avoids a lot of annoying little
errors like this one.

Cheers,
Cameron Simpson cs@cskk.id.au

Since I’m learning from examples I find on the Internet, I now know that instead of following their example of IMPORT BLAH it should be FROM BLAH IMPORT A,B,C.