问题描述
我正在尝试使用python 3中的线程功能来使自己的pingtesting应用程序/日志正常工作,所以我遵循 YouTube教程
I'm experimenting with the threading function in python 3 to get my own pingtesting app/log working, so im following a youtube tutorial
启动python 3解释器并运行:
When I've launched a python 3 interpreter, and run:
>>> import threading
>>> print_lock = threading.Lock()
它正确返回
>>> print_lock
<_thread.lock object at 0x042093C8>
但是当我在脚本中使用那段代码并尝试以
But when I use that piece of code in a script and try to run it as
python scriptName.py
我收到一条错误消息,说属性Lock()不存在
I get an error saying the attribute Lock() doesn't exist
AttributeError: 'module' object has no attribute 'Lock'
这怎么可能?我已经验证了在运行python解释器时会返回什么threading.Lock(),为什么当我尝试在脚本中运行它时为什么无法识别它?我该如何运行它?
How is this possible? I've verified what threading.Lock() returns when running the python interpreter, why isn't it recognized when I try to run it in a script and how can I get this running?
推荐答案
您是否偶然为模块(或工作目录中的另一个模块)命名了threading.py
?它将在内置threading
之前导入,从而导致此确切问题.
Did you happen to name your module (or another module in the working directory) threading.py
? It would get imported ahead of the built-in threading
, causing this exact problem.
尝试运行:
print(threading.__file__)
在您的模块中,我怀疑您会发现它不是Python内置的.
in your module, I suspect you'll find it's not the Python built-in.
这篇关于threading.Lock()无法通过脚本工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!