Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indent python file (with pydev) in eclipse

I'm a newbie in eclipse. I want to indent all the lines of my code and formatting the open file by pressing a shortcut or something like that... I know the CTRL+SHIFT+F (as it actually doesn't work in pydev!!) I've been searching for hours with no success. Is there any way to do that in eclipse. kind of like CTRL+K,D in visual studio, which formats and indents all the source code lines automatically?

like image 678
Saied_Delshad Avatar asked Oct 04 '11 21:10

Saied_Delshad


People also ask

How do you indent a Python script?

Use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation: 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.

How do I open PyDev in Eclipse?

Go to Window → Open Perspective → Other and choose PyDev, then click OK. If you look at the upper right corner you will see that the perspective has changed from "Java" to "PyDev".


2 Answers

If you want to change from 2 space to 4 space indentation (for instance), use "Source->Convert space to tab" with 2 spaces, then "Soruce->Convert tab to space" with 4 spaces.

like image 189
Demyn Avatar answered Sep 27 '22 21:09

Demyn


I ... don't think this question makes sense. Indentation is syntax in Python. It doesn't make sense to have your IDE auto-indent your code. If it's not indented properly already, it doesn't work, and the IDE can't know where your indentation blocks begin and end. Take, for example:

# Valid Code
for i in range(10):
  b = i

for j in range(b):
  c = j

# Also Valid Code.
for i in range(10):
  b = i

  for j in range(b):
    c = j

There's no possible way that the IDE can know which of those is the correct version, or what your intent is. If you're going to write Python code, you're going to have to learn to manage the indentation. There's no way to avoid it, and expecting the IDE to magically clean it up and still get the desired result out of it is pretty much impossible.

Further example:

# Valid Code.
outputData = []

for i in range(100):
  outputData.append(str(i))

print ''.join(outputData)

# Again, also valid code, wildly different behavior.
outputData = []

for i in range(100):
  outputData.append(str(i))

  print ''.join(outputData)

The first will produce a list of strings, then print the joined result to the console 1 time. The second will still produce a list of strings, but prints the cumulative joined result for each iteration of the loop - 100 print statements. The two are both 100% syntactically correct. There's no problem with them. Either of them could be what the developer wanted. An IDE can't "know" which is correct. It could, very easily incorrectly change the first version to the second version. Because the Language uses Indentation as Syntax, there is no way to configure an IDE to perform this kind of formatting for you.

like image 26
g.d.d.c Avatar answered Sep 27 '22 21:09

g.d.d.c