【问题标题】:How to remove bad indentation error ? eclipse python如何消除不良缩进错误?日食蟒蛇
【发布时间】:2015-01-16 16:35:03
【问题描述】:

我在 Eclipse 中使用 pyDev。 我将外部代码导入到我的项目中,我收到了错误的缩进警告,这是因为外部代码的缩进与我的代码不同。

我该如何解决这个问题?

【问题讨论】:

  • 有些程序使用制表符,有些程序使用空格来创建缩进。我通常使用 Notepad++(查看->显示符号->显示所有字符)来显示缩进并手动修复它。但是我可以想象当你有很多行代码时这有点乏味。
  • 是的,我有很多行代码,我无法手动完成!
  • 你可能必须这样做:stackoverflow.com/questions/407929/…
  • 这只是一个警告,忽略它或者看看有没有办法关闭它。

标签: python eclipse python-2.7 pydev


【解决方案1】:

“Bad indentaion”警告可以通过 Preferences -> PyDev -> Editor -> Code Analysis -> Others -> “indentation questions and blend of tabs and spaces”关闭。

一旦值设置为“忽略”。这些标记应该消失。 :)

【讨论】:

    【解决方案2】:

    %PYTHON%/Tools/Scripts 文件夹中安装了一个名为reindent.py 的脚本。我自己没有使用过,但根据文档,它将您的代码缩进修复为 4 个空格:

    reindent [-d][-r][-v] [ path ... ]
    
    -d (--dryrun)   Dry run.   Analyze, but don't make any changes to, files.
    -r (--recurse)  Recurse.   Search for all .py files in subdirectories too.
    -n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
    -v (--verbose)  Verbose.   Print informative msgs; else no output.
    -h (--help)     Help.      Print this usage information and exit.
    
    Change Python (.py) files to use 4-space indents and no hard tab characters.
    Also trim excess spaces and tabs from ends of lines, and remove empty lines
    at the end of files.  Also ensure the last line ends with a newline.
    
    If no paths are given on the command line, reindent operates as a filter,
    reading a single source file from standard input and writing the transformed
    source to standard output.  In this case, the -d, -r and -v flags are
    ignored.
    
    You can pass one or more file and/or directory paths.  When a directory
    path, all .py files within the directory will be examined, and, if the -r
    option is given, likewise recursively for subdirectories.
    
    If output is not to standard output, reindent overwrites files in place,
    renaming the originals with a .bak extension.  If it finds nothing to
    change, the file is left alone.  If reindent does change a file, the changed
    file is a fixed-point for future runs (i.e., running reindent on the
    resulting .py file won't change it again).
    
    The hard part of reindenting is figuring out what to do with comment
    lines.  So long as the input files get a clean bill of health from
    tabnanny.py, reindent should do a good job.
    
    The backup file is a copy of the one that is being reindented. The ".bak"
    file is generated with shutil.copy(), but some corner cases regarding
    user/group and permissions could leave the backup file more readable than
    you'd prefer. You can always use the --nobackup option to prevent this.
    

    在 Python 源代码分发中,它位于 Tools/scripts 目录中,而在 Ubuntu 中,您可以安装 python2.7-examplespython3.4-examples 软件包。

    【讨论】:

    • 这不是一个仅链接的答案,reindent.py 随附于 Windows Python(尽管未安装在 Ubuntu 中);如果外部代码导入只是单向的(也就是不会推送补丁),那么为什么不修复缩进并保留缩进警告(甚至将缩进问题设置为 Error)。 OTOH,PyDev 也应该这样做。
    • @cfi:这不是仅链接的答案。它解释说有一个脚本可以重新缩进 Python 源文件,就在答案中
    • 我没有注意到你的链接。请再次修改我的通知。
    • @AnttiHaapala:Ubuntu 将其打包在 pythonx.x-examples 包中。例如,请参阅Python 3.4 examples package
    • @AnikIslamAbhi:但这不是我的答案。 :-P
    猜你喜欢
    • 2023-02-16
    • 2021-03-22
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2019-05-05
    相关资源
    最近更新 更多