使用“threading”模块时出现问题 - AttributeError:模块“threading”没有属性“RLock”

Too*_*one 3 python multithreading python-3.x

我收到以下错误:

AttributeError: module 'threading' has no attribute 'RLock'
Exception ignored in: <module 'threading' from 'D:\\PYTHON_STACKOVERFLOW_ANSWERS\\threading.py'>
AttributeError: module 'threading' has no attribute '_shutdown'
Run Code Online (Sandbox Code Playgroud)

我的代码如下所示:

import logging
import threading
import time

def thread_function(name):
    logging.info("Thread %s: starting", name)
    time.sleep(2)
    logging.info("Thread %s: finishing", name)


if __name__ == "__main__":
    format = "%(asctime)s: %(message)s"
    logging.basicConfig(
        format=format,
        level=logging.INFO,
        datefmt="%H:%M:%S"
    )
    logging.info("Main    : before creating thread")
    x = threading.Thread(target=thread_function, args=(1,))
    logging.info("Main    : before running thread")
    x.start()
    logging.info("Main    : wait for the thread to finish")
    # x.join()
    logging.info("Main    : all done")
Run Code Online (Sandbox Code Playgroud)

堆栈溢出抱怨道:“看来您的帖子主要是代码;请添加更多详细信息。” 所以我要把这一点文字放在最后;对不起。

Too*_*one 9

如果出现此错误,则意味着您可能命名了自己的文件之一threading.py,因此导入会导入您自己的文件,而不是导入预期的库。

虽然这是一个愚蠢的错误,但希望有人在谷歌上搜索“ AttributeError: module 'threading' has no attribute 'RLock'”会发现这个答案有帮助。