如果 X 库是在我在当前文件中导入的 Y 模块中导入的,我是否需要导入 X 库?

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

比如我有date_file.py:

import datetime

EPOCH = datetime.datetime.utcfromtimestamp(0)

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

我有 utils.py:

import date_file

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

print(ux)

但是当我 运行 utils.py 它说

"name datetime is not defined"

到处导入这些模块是不是有点多余?对此有更好的解决方案吗?

谢谢!

在 python 中,您还需要导入元素。您所做的只是导入文件本身,而不是其中的内容。

要导入元素,您必须在文件中使用这一行 utils.py

from date_file import *