WinPython:“NameError:未定义名称‘时间’",但仅在函数内部 [英] WinPython: "NameError: name 'time' is not defined", but only inside a function

查看:67
本文介绍了WinPython:“NameError:未定义名称‘时间’",但仅在函数内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WinPython 通过 USB 闪存驱动器运行我的 Python 程序.但是当我尝试在我创建的函数中运行导入的函数时,我收到以下错误(任何导入的模块都会发生):

I'm using WinPython to run my Python program using a USB flash drive. But when I try to run an imported function inside a function I created, I receive the following error (it happens with any imported module):

NameError: name 'time' 未定义

NameError: name 'time' is not defined

这是一个很好的例子:

import time
time.sleep(0.5)

这是一个不起作用的例子:

Here is an example 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 解释器执行相同的程序,则该程序运行良好.但是我需要使用安装在我的 U 盘中的 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 USB flash drive.

推荐答案

通过使用 spyder.exe(包含在 WinPython 中的 IDE)执行程序解决了该问题.之前它不起作用,因为我试图通过安装在我的 USB 闪存驱动器上的 Python 解释器直接执行程序.

The problem was solved by executing the program using spyder.exe (an IDE included in WinPython). It wasn't working before because I was trying to execute the program directly by the Python interpreter installed on my USB flash drive.

这篇关于WinPython:“NameError:未定义名称‘时间’",但仅在函数内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆