调试含os.path.relpath()的Python文件遇AttributeError问题求助
解决Windows 11下PyCharm调试pytest时出现的
AttributeError: module 'ntpath' has no attribute 'sep'问题 环境信息
- 系统:Windows 11 22H2 22621.674
- IDE:PyCharm Community Edition 2022.2.3
- Python版本:3.10.6
- pytest版本:7.1.2
- 环境类型:Conda虚拟环境
错误现象
调试调用了os.path.relpath()的Python文件时,触发如下错误:
AttributeError: module 'ntpath' has no attribute 'sep'
完整报错堆栈:
collecting ... collected 0 items / 2 errors INTERNALERROR> Traceback (most recent call last): INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\_pytest\\main.py", line 268, in wrap_session INTERNALERROR> session.exitstatus = doit(config, session) or 0 INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\_pytest\\main.py", line 321, in _main INTERNALERROR> config.hook.pytest_collection(session=session) INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_hooks.py", line 265, in __call__ INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult) INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_manager.py", line 80, in _hookexec INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult) INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_callers.py", line 60, in _multicall INTERNALERROR> return outcome.get_result() INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_result.py", line 60, in get_result INTERNALERROR> raise ex[1].with_traceback(ex[2]) INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_callers.py", line 39, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\_pytest\\main.py", line 332, in pytest_collection INTERNALERROR> session.perform_collect() INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\_pytest\\main.py", line 644, in perform_collect INTERNALERROR> self.ihook.pytest_collectreport(report=rep) INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_hooks.py", line 265, in __call__ INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult) INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_manager.py", line 80, in _hookexec INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult) INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_callers.py", line 60, in _multicall INTERNALERROR> return outcome.get_result() INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_result.py", line 60, in get_result INTERNALERROR> raise ex[1].with_traceback(ex[2]) INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\pluggy\\_callers.py", line 39, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> File "C:\\Program Files\\JetBrains\\PyCharm Community Edition 2022.2.3\\plugins\\python-ce\\helpers\\pycharm\\teamcity\\pytest_plugin.py", line 328, in pytest_collectreport INTERNALERROR> self.report_test_failure(test_id, report) INTERNALERROR> File "C:\\Program Files\\JetBrains\\PyCharm Community Edition 2022.2.3\\plugins\\python-ce\\helpers\\pycharm\\teamcity\\pytest_plugin.py", line 223, in report_test_failure INTERNALERROR> self.ensure_test_start_reported(test_id) INTERNALERROR> File "C:\\Program Files\\JetBrains\\PyCharm Community Edition 2022.2.3\\plugins\\python-ce\\helpers\\pycharm\\teamcity\\pytest_plugin.py", line 186, in ensure_test_start_reported INTERNALERROR> self.teamcity.testStarted(test_id, flowId=test_id, captureStandardOutput=capture_standard_output, metainfo=metainfo) INTERNALERROR> File "C:\\Program Files\\JetBrains\\PyCharm Community Edition 2022.2.3\\plugins\\python-ce\\helpers\\pycharm\\_jb_runner_tools.py", line 181, in testStarted INTERNALERROR> commands = _TREE_MANAGER_HOLDER.manager.level_opened(self._test_to_list(testName), _write_start_message) INTERNALERROR> File "C:\\Program Files\\JetBrains\\PyCharm Community Edition 2022.2.3\\plugins\\python-ce\\helpers\\pycharm\\_jb_runner_tools.py", line 51, in manager INTERNALERROR> self._fill_manager() INTERNALERROR> File "C:\\Program Files\\JetBrains\\PyCharm Community Edition 2022.2.3\\plugins\\python-ce\\helpers\\pycharm\\_jb_runner_tools.py", line 59, in _fill_manager INTERNALERROR> from _jb_serial_tree_manager import SerialTreeManager INTERNALERROR> File "<frozen importlib._bootstrap>", line 1027, in _find_and_load INTERNALERROR> File "<frozen importlib._bootstrap>", line 1002, in _find_and_load_unlocked INTERNALERROR> File "<frozen importlib._bootstrap>", line 945, in _find_spec INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\_pytest\\assertion\\rewrite.py", line 92, in find_spec INTERNALERROR> if self._early_rewrite_bailout(name, state): INTERNALERROR> File "C:\\ProgramData\\Anaconda3\\envs\\my2ndconda\\lib\\site-packages\\_pytest\\assertion\\rewrite.py", line 193, in _early_rewrite_bailout INTERNALERROR> path = PurePath(os.path.sep.join(parts) + ".py") INTERNALERROR> AttributeError: module 'ntpath' has no attribute 'sep'
排查结论
问题根源在于当前使用的Conda虚拟环境存在异常。
解决方案
方案1:重建Conda虚拟环境
- 删除当前异常环境:
conda remove -n my2ndconda --all - 创建全新虚拟环境并安装依赖:
conda create -n new_env python=3.10.6 conda activate new_env pip install pytest==7.1.2 # 安装项目其他依赖 - 在PyCharm中切换解释器到新环境,重新调试。
方案2:修复Conda环境中的Python标准库
- 验证ntpath模块状态:
在环境中执行以下代码,检查模块是否正常:import ntpath print(ntpath.sep) - 如果报错,说明标准库损坏,执行强制重装:
conda install python=3.10.6 --force-reinstall
方案3:禁用PyCharm的TeamCity插件(临时 workaround)
从报错堆栈看,错误触发与PyCharm的TeamCity pytest插件相关,可尝试禁用:
- 打开PyCharm的
Settings/Preferences - 进入
Plugins,找到TeamCity Integration插件并禁用 - 重启PyCharm后重新调试
内容的提问来源于stack exchange,提问作者forestbat
相关产品推荐
相关产品推荐

