[Python] AttributeError: module 'thread' has no attribute 'start_new_thread'

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get data from an endpoint on a website using Python, here is my code:

    from aiohttp import ClientSession as session

    async def get_api_data(api_url):
    try:
    async with session.post(api_url, data={"limit": 10000, "page": 1}) as response:
    response.raise_for_status()
    data = asyncio.run(response.json())
    return data
    except session as e:
    print(f"Error getting API data: {e}")
    return None

    asyncio.create_task(get_api_data("url"))


    But I am getting this error:

    Traceback (most recent call last):
    File "c:\Users\abdul\AppData\Local\Programs\Python\Python312\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "c:\Users\abdul\AppData\Local\Programs\Python\Python312\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
    cli.main()
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 268, in run_file
    start_debugging(target)
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 258, in start_debugging
    debugpy.connect(options.address, access_token=options.adapter_access_token)
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\public_api.py", line 31, in wrapper
    return wrapped(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\api.py", line 143, in debug
    log.reraise_exception("{0}() failed:", func.__name__, level="info")
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\api.py", line 141, in debug
    return func(address, settrace_kwargs, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\api.py", line 297, in connect
    _settrace(host=host, port=port, client_access_token=access_token, **settrace_kwargs)
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\api.py", line 45, in _settrace
    return pydevd.settrace(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\pydevd.py", line 2821, in settrace
    _locked_settrace(
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\pydevd.py", line 2942, in _locked_settrace
    py_db.patch_threads()
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\pydevd.py", line 2392, in patch_threads
    patch_thread_modules()
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 1205, in patch_thread_modules
    patch_thread_module(t)
    File "c:\Users\abdul\.vscode-insiders\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 1168, in patch_thread_module
    _original_start_new_thread = thread_module._original_start_new_thread = thread_module.start_new_thread
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: module 'thread' has no attribute 'start_new_thread'


    When I checked the debugpy.py file, here are the lines I found: I put a comment before the line causing the error, it is in the 8th line.

    def patch_thread_module(thread_module):

    if getattr(thread_module, '_original_start_new_thread', None) is None:
    if thread_module is threading:
    if not hasattr(thread_module, '_start_new_thread'):
    return # Jython doesn't have it.
    _original_start_new_thread = thread_module._original_start_new_thread = thread_module._start_new_thread
    else:
    #-----------This is the line causing the error--------
    _original_start_new_thread = thread_module._original_start_new_thread = thread_module.start_new_thread
    else:
    _original_start_new_thread = thread_module._original_start_new_thread

    class ClassWithPydevStartNewThread:

    def pydev_start_new_thread(self, function, args=(), kwargs={}):
    '''
    We need to replace the original thread_module.start_new_thread with this function so that threads started
    through it and not through the threading module are properly traced.
    '''
    return _original_start_new_thread(_UseNewThreadStartup(function, args, kwargs), ())

    # This is a hack for the situation where the thread_module.start_new_thread is declared inside a class, such as the one below
    # class F(object):
    # start_new_thread = thread_module.start_new_thread
    #
    # def start_it(self):
    # self.start_new_thread(self.function, args, kwargs)
    # So, if it's an already bound method, calling self.start_new_thread won't really receive a different 'self' -- it
    # does work in the default case because in builtins self isn't passed either.
    pydev_start_new_thread = ClassWithPydevStartNewThread().pydev_start_new_thread

    try:
    # We need to replace the original thread_module.start_new_thread with this function so that threads started through
    # it and not through the threading module are properly traced.
    if thread_module is threading:
    thread_module._start_new_thread = pydev_start_new_thread
    else:
    thread_module.start_new_thread = pydev_start_new_thread
    thread_module.start_new = pydev_start_new_thread
    except:
    pass


    def patch_thread_modules():
    for t in threading_modules_to_patch:
    patch_thread_module(t)


    def undo_patch_thread_modules():
    for t in threading_modules_to_patch:
    try:
    t.start_new_thread = t._original_start_new_thread
    except:
    pass

    try:
    t.start_new = t._original_start_new_thread
    except:
    pass

    try:
    t._start_new_thread = t._original_start_new_thread
    except:
    pass



    I updated Python (installed version 3.12.2), updated the libraries I am using, but the issue was not resolved. Can you please help me with this?

    Continue reading...

Compartilhe esta Página