WinPython:NameError:仅在函数内部未定义名称“时间" [英] WinPython: NameError: name 'time' is not defined only inside a function
问题描述
我正在使用WinPython通过PenDrive运行python程序. 但是,当我尝试在创建的函数中运行导入的函数时,出现以下错误: NameError:未定义名称时间"(发生在任何导入的模块中)
I'm using WinPython to run my python program using a pendrive. But when i try to run an imported function inside a function i created, i receave the following error: NameError: name 'time' is not defined (it happens with any imported module)
这里有一个很好的例子:
Here an example that works fine:
import time
time.sleep(0.5)
这是一个不起作用的示例:
Here an exemple that doesn't work:
import time
def waitLoad():
time.sleep(0.2)
waitLoad()
在执行上述示例时,我收到以下错误消息:
I receive the following error executing the example above:
Traceback (most recent call last):
File "E:\WinPython\python-3.6.5.amd64\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "E:\WinPython\python-3.6.5.amd64\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "E:\WinPython\python-3.6.5.amd64\scripts\ptpython.exe\__main__.py", line 9, in <module>
File "E:\WinPython\python-3.6.5.amd64\lib\site-packages\ptpython\entry_points\run_ptpython.py", line 55, in run
six.exec_(compile(open(a['<arg>'][0], "rb").read(), a['<arg>'][0], 'exec'))
File "../testeMouse.py", line 4, in <module>
waitLoad()
File "../testeMouse.py", line 3, in waitLoad
time.sleep(0.2)
NameError: name 'time' is not defined
如果我使用计算机中安装的python解释器执行相同的程序,则该程序可以正常运行.但是我需要使用安装在pendrive中的python解释器来执行它.
If i execute the same program using the python interpreter installed in my computer, the program works fine. But I need to execute it using the python interpreter installed in my pendrive.
推荐答案
问题是通过使用spyder.exe(WinPython中包含的IDE)执行程序解决的.以前没有用,因为我试图直接通过笔式驱动器上安装的python解释器直接执行程序.
The problem was solved by executing the program using spyder.exe (IDE included in WinPython). It wasn't working before because i was trying to execute the program directly by the python intepreter installed on my pendrive.
感谢用户@roganjosh帮我弄清楚了.
Thanks to the user @roganjosh that helped me figure it out.
这篇关于WinPython:NameError:仅在函数内部未定义名称“时间"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!