首页 > 解决方案 > 不会注册或允许我在 python 中导入时间模块

问题描述

我正在尝试将时间模块导入到我的 python 代码中,但它不将时间识别为模块。我正在使用 Visual Studio 代码。

我可以使用其他模块,例如随机,并且效果很好。我试过只导入 sleep 命令,但这也不起作用
。)

from time import sleep

print ('hi)
time.sleep(1)
print ('hello')

但不幸的是,这也不起作用。当我运行上一个时,它返回以下内容:

Traceback (most recent call last):
  File "c:/Users/Jacob/Repositories/python/testing.py", line 4, in <module>
    time.sleep(1)
NameError: name 'time' is not defined

下面的代码是我想在我的代码中使用的代码,但仍然无法正常工作并给出相同的错误。为了澄清我正在尝试使用时间模块,而python在使用前面和后面的代码时没有将它识别为模块。

#this is just an example for what I need for my program.
import time

print ('hi')
time.sleep(1)
print ('hello')

标签: pythontimemodule

解决方案


如果问题出在导入上,您可能需要安装 time 包

一旦你拥有它,这应该工作:

from time import sleep

print ('hi')
sleep(1)
print ('hello')

推荐阅读