简体   繁体   中英

Do I need to import X library, if it's imported within a Y module I'm importing in the current file?

For example, I have date_file.py:

import datetime

EPOCH = datetime.datetime.utcfromtimestamp(0)

def date_to_unix(dt):
    return (dt - EPOCH).total_seconds() * 1000.0

and I have utils.py:

import date_file

ux = date_file.date_to_unix(datetime.datetime(2020,3,27,0,0,0))

print(ux)

But when I run utils.py it says

"name datetime is not defined"

Isn't it a bit redundant to import these modules everywhere? Is there a better solution for this?

Thanks!

In python you need to import the elements as well. What you did was just import the file itself, but not what is in it.

To import the elements you must use this line in your file utils.py

from date_file import *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM