我如何解决 NameError: name 'threading' is not defined in python 3.3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22848621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:48:51  来源:igfitidea点击:

How do I solve NameError: name 'threading' is not defined in python 3.3

pythonpython-3.x

提问by Chris F

I have the following program, and nothing else, python 3.3. When I run it. I get

我有以下程序,没有别的,python 3.3。当我运行它时。我得到

NameError: name 'threading' is not defined

I googled but none of the answers given explain my situation. any clues? Thanks!

我用谷歌搜索,但给出的答案都没有解释我的情况。任何线索?谢谢!

#!/usr/bin/python

import Utilities
import os
import sys
import getopt
import time
from queue import Queue
from threading import Thread

_db_lock=threading.Lock()

I also tried

我也试过

_db_lock=threading.Lock

采纳答案by msvalkon

You must import threading. Add the following to the beginning of your file:

您必须导入线程。将以下内容添加到文件的开头:

import threading

The error originates from the line:

错误源自以下行:

_db_lock=threading.Lock()

That's because you've used from threading import Thread, but you've never actually introduced threadingin to the local namespace. So far there's only Thread(even though technically the import is there, it's just not in the namespace, you cannot use it).

那是因为您已经使用了from threading import Thread,但您实际上从未将其引入threading本地命名空间。到目前为止只有Thread(即使从技术上来说导入是存在的,它只是不在命名空间中,你不能使用它)。

If for some reason you wish to keep threadingfrom 'polluting' your namespace, import the Lockin the same manner as you've imported Thread, like so:

如果出于某种原因您希望threading避免“污染”您的命名空间,请Lock以与导入相同的方式导入Thread,如下所示:

from threading import Thread, Lock
_db_lock = Lock()