You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止VS Code自动拆分Python的print语句?

解决VS Code中Python print语句被自动换行的问题

问题出在你使用的Python代码格式化工具(比如Black、autopep8、yapf)的行宽设置上,VS Code自身的换行宽度设置不会覆盖这些工具的配置,所以改到500也没用。按以下步骤调整:

  • 先确认当前用的格式化工具:打开VS Code设置,搜索Python Formatting Provider,查看选中的是哪一个(black/autopep8/yapf)。

  • 针对对应工具修改行宽配置:

    1. 如果是Black:
      • 方法一:在VS Code设置中搜索Python > Formatting: Black Args,将值设置为 ["--line-length", "500"]。
      • 方法二:在项目根目录创建pyproject.toml文件,添加以下内容:
        [tool.black]
        line-length = 500
        
    2. 如果是autopep8:
      • 方法一:在VS Code设置中搜索Python > Formatting: Autopep8 Args,设置为 ["--max-line-length", "500"]。
      • 方法二:在项目根目录创建.autopep8文件,写入:
        [autopep8]
        max_line_length = 500
        
    3. 如果是yapf:
      • 方法一:在VS Code设置中搜索Python > Formatting: Yapf Args,设置为 ["--style={based_on_style: pep8, column_limit: 500}"]。
      • 方法二:在项目根目录创建.style.yapf文件,添加:
        [style]
        based_on_style = pep8
        column_limit = 500
        

修改后保存设置,再重新格式化代码,print语句就不会被强制拆分成两行了。

内容的提问来源于stack exchange,提问作者Jake Siney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:30:57