Python VSCode remote debug not working

Hello,

I’m trying to get VSCode to attach to unreals python instance, but im getting weird behaviour from unreal when im trying to start the listen server.

I did all the necessary things to setup python for unreal (I can execute python scripts from within unreal perfectly fine).

However when I try to execute this script below, a new instance of Unreal starts. So I suddenly have two editors and after a moment the output log in the original editor screams in pain and I get this output below the script.

I have no clue why unreal starts a new instance tho. It is a mystery.
However what I do know is that I can open up a listen server that I can actually connect to with the package called ptvsd. Which is also used for debugging python but it’s deprecated and even recommends using the microsoft version debugpy

I did install both packages for the python interpreter that is shipped with the unreal engine version I am using.
Executing the script with the debugpy version outside of unreal works, but that’s not what I need.

init_remote_debug.py

def attach_to_debugger(host, port):
    try:
        import debugpy

        debugpy.listen((host, port))
        debugpy.wait_for_client()
        
        print("VSCode Remote Debug enabled on %s:%s." % (host,port))
    except:
        import traceback
        traceback.print_exc()

attach_to_debugger("0.0.0.0", 60058)

Output Log

Cmd: py "C:/Users/Mark/Documents/Unreal Projects/PythonTest/Content/Python/init_remote_debug.py"
LogPython: Error: Traceback (most recent call last):
LogPython: Error:   File "F:\Games\Epic\UE_4.27\Engine\Binaries\ThirdParty\Python3\Win64\lib\site-packages\debugpy\server\api.py", line 215, in listen
    sock, _ = endpoints_listener.accept()
LogPython: Error:   File "F:\Games\Epic\UE_4.27\Engine\Binaries\ThirdParty\Python3\Win64\lib\socket.py", line 212, in accept
    fd, addr = self._accept()
LogPython: Error: socket.timeout: timed out
LogPython: Error: During handling of the above exception, another exception occurred:
LogPython: Error: Traceback (most recent call last):
LogPython: Error:   File "C:/Users/Mark/Documents/Unreal Projects/PythonTest/Content/Python/init_remote_debug.py", line 5, in attach_to_debugger
    debugpy.listen((host, port))
LogPython: Error:   File "F:\Games\Epic\UE_4.27\Engine\Binaries\ThirdParty\Python3\Win64\lib\site-packages\debugpy\public_api.py", line 31, in wrapper
    return wrapped(*args, **kwargs)
LogPython: Error:   File "F:\Games\Epic\UE_4.27\Engine\Binaries\ThirdParty\Python3\Win64\lib\site-packages\debugpy\server\api.py", line 140, in debug
    log.reraise_exception("{0}() failed:", func.__name__, level="info")
LogPython: Error:   File "F:\Games\Epic\UE_4.27\Engine\Binaries\ThirdParty\Python3\Win64\lib\site-packages\debugpy\server\api.py", line 138, in debug
    return func(address, settrace_kwargs, **kwargs)
LogPython: Error:   File "F:\Games\Epic\UE_4.27\Engine\Binaries\ThirdParty\Python3\Win64\lib\site-packages\debugpy\server\api.py", line 229, in listen
    raise RuntimeError("timed out waiting for adapter to connect")
LogPython: Error: RuntimeError: timed out waiting for adapter to connect