问题背景以及问题描述:

在debug 过程中,出现了 Connection to Python debugger failed Socket closed 错误具体报错为:

Traceback (most recent call last):   File "/home/Downloads/pycharm-community-2022.3.1/plugins/python-ce/helpers/pydev/pydevd.py", line 45, in     from _pydevd_bundle.pydevd_comm import CMD_SET_BREAK, CMD_SET_NEXT_STATEMENT, CMD_STEP_INTO, CMD_STEP_OVER, \   File "/home/Downloads/pycharm-community-2022.3.1/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 91, in     from _pydevd_bundle import pydevd_console_integration   File "/home/Downloads/pycharm-community-2022.3.1/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_console_integration.py", line 21, in     from _pydevd_asyncio_util.pydevd_nest_asyncio import apply   File "/home/Downloads/pycharm-community-2022.3.1/plugins/python-ce/helpers/pydev/_pydevd_asyncio_util/pydevd_nest_asyncio.py", line 33, in     import asyncio   File "/home/anaconda3/envs/pyslowfast/lib/python3.10/asyncio/__init__.py", line 8, in     from .base_events import *   File "/home/anaconda3/envs/pyslowfast/lib/python3.10/asyncio/base_events.py", line 18, in     import concurrent.futures   File "/home/anaconda3/envs/pyslowfast/lib/python3.10/concurrent/futures/__init__.py", line 8, in     from concurrent.futures._base import (FIRST_COMPLETED,   File "/home/anaconda3/envs/pyslowfast/lib/python3.10/concurrent/futures/_base.py", line 7, in     import logging   File "/home/PYTHON-PROJ/Contrast_learing/SAIC_MAEST/util/logging.py", line 12, in     import torch   File "/home/anaconda3/envs/pyslowfast/lib/python3.10/site-packages/torch/__init__.py", line 457, in     for name in dir(_C): NameError: name '_C' is not defined

Process finished with exit code 1

因此下意识我想run它(省略写)依然有错误 ,那么先解决 run 的问题:

    LOGGER = logging.getLogger("concurrent.futures") AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)

Process finished with exit code 1

解决过程及问题成因:

看到问题的核心是(most likely due to a circular import) 即为一个 循环的引用

那么大概率就是logging 这个模块有 循环引用

果然文件同路径下有 logging.py 并且 在 logging.py 中 还 import 了 logging,造成了循环引用

解决方案:

更改 名字 避免循环引用:

把原logging.py 更名为logging1.py

即可解决问题

结论及重点:

造成 Connection to Python debugger failed Socket closed 的原因会有很多,需要具体去看错误,循序渐进即可解决,在本例(most likely due to a circular import)中造成该情况的原因则是循环引用

好文阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。