问题
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
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.
回答1:
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.
Thanks to the user @roganjosh that helped me figure it out.
来源:https://stackoverflow.com/questions/51126979/winpython-nameerror-name-time-is-not-defined-only-inside-a-function