0

I am using pyDev with eclipse. I import external code to my project, and I got bad indentation warning , that's because, the external code has different indentation than my code.

How could I fix that ?

david
  • 45
  • 2
  • 7
  • Some programs use tabs, other use spaces to create indentation. I generally use Notepad++ (View->Show Symbol->Show all characters) to show the indentation and fix that manually. But I can imagine that is a bit tedious when you have many lines of code. – Rik Verbeek Nov 19 '14 at 08:34
  • yes I have many lines of code, I couldn't do that manually ! – david Nov 19 '14 at 08:36
  • You probably have to do something like this: http://stackoverflow.com/questions/407929/how-do-i-change-eclipse-to-use-spaces-instead-of-tabs – Dyrborg Nov 19 '14 at 08:39
  • It's just a warning, ignore it or see if there's a way to turn it off. – martineau Nov 19 '14 at 09:14

2 Answers2

2

There is a script installed in %PYTHON%/Tools/Scripts folder called reindent.py. I haven't used it myself, but according to the documentation it fixes the indentation of your code to 4 spaces:

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.

In a Python source distribution it is located in the Tools/scripts directory, and in Ubuntu you can install the python2.7-examples or python3.4-examples packages.

Antti Haapala
  • 117,318
  • 21
  • 243
  • 279
Rob
  • 3,019
  • 1
  • 17
  • 26
  • This is not a link-only answer, the `reindent.py` comes with Windows Python (though not installed in Ubuntu); if the external code import is just one-way (a.k.a. no patches will be pushed), then why not fix the indentation and leave the indentation warnings on (or even set the indentation problems to *Error*). OTOH, PyDev ought to do this too. – Antti Haapala Apr 02 '15 at 05:35
  • @cfi: this is **not** a link-only answer. It explains that there is a script that reindents Python source files, *right here in the answer*. – Martijn Pieters Apr 02 '15 at 09:03
  • I didn't notice you for link . Please revise again my notification. – Anik Islam Abhi Apr 02 '15 at 09:06
  • 1
    @AnttiHaapala: Ubuntu packages it in the `pythonx.x-examples` packages. See the [Python 3.4 examples package](http://packages.ubuntu.com/trusty/python3.4-examples) for example. – Martijn Pieters Apr 02 '15 at 09:06
  • Now your answer is ok . I removed my notification :) – Anik Islam Abhi Apr 02 '15 at 09:18
  • 2
    @AnikIslamAbhi: it's not my answer though. :-P – Martijn Pieters Apr 02 '15 at 09:18
  • @Martijn: Indeed! My fault. – cfi Apr 02 '15 at 20:35
2

"Bad indentaion" warning could be turned off with Preferences -> PyDev -> Editor -> Code Analysis -> Others -> "indentation problems and mixing of tabs and spaces".

Once the value is set to "ignore". Those markers should go away. :)

enter image description here

Paul H.
  • 1,054
  • 1
  • 8
  • 8