python - 不会注册或允许我在 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')
解决方案
如果问题出在导入上,您可能需要安装 time 包
一旦你拥有它,这应该工作:
from time import sleep
print ('hi')
sleep(1)
print ('hello')
推荐阅读
- html - 如何使用 flexbox 或网格进行此设计?
- node.js - type 'typeof globalThis' 没有索引签名
- python - Python 和 Beautifulsoup 使用链接提取多个 li 项目及其锚文本
- html - 锚标签内的按钮标签不起作用?
- python - 过滤后尝试将文件从子文件夹传递到新文件夹
- terraform - 使用 AWS 提供商的 Terraform 无法创建 CodeBuild
- javascript - 访问不存在的文档时,Firebase 事务返回“失败的前提条件”错误
- typescript - 需要帮助将类型添加到事件总线方法
- javascript - 了解 GSAP 手风琴
- iot - Micropython:https 请求阻止进一步的请求