90

A little background:

  • PEP 8 is the Style Guide for Python Code. It contains the conventions all python programmers should follow.
  • pep8.py is a (very useful) script that checks the code formating of a given python script, according to PEP 8.
  • Eclipse is a great IDE. With the Pydev extension, it that can be used to develop Python

I run pep8.py manually when I'm scripting, but with bigger projects I prefer to use Eclipse. It would be really useful to integrate pep8.py in Eclipse/Pydev, so it can be run automatically in all the files in the project, and point to the lines containing the warnings. Maybe there is an obvious way to do it, but I haven't found it yet.

Question is: How to integrate pep8.py in Eclipse?

AlexA
  • 4,028
  • 8
  • 51
  • 84
David Arcos
  • 5,957
  • 5
  • 30
  • 39
  • 3
    There's now a default pep8 integration. See: http://stackoverflow.com/questions/399956/how-to-integrate-pep8-py-in-eclipse/8532188#8532188 – Fabio Zadrozny Jan 01 '12 at 14:10
  • 1
    Given that the situation has changed since you accepted [Dmitry Kochkin](http://stackoverflow.com/a/2296249/42473)'s answer, could I suggest that you change your accepted answer to either [Fabio Zadrozny](http://stackoverflow.com/a/8532188/42473)'s answer or [angelo.bixly](http://stackoverflow.com/a/8830316/42473)'s answer? This will help future visitors who, like myself, came to this question from Google, allowing us to find the most up to date information first - otherwise it may take a long time for these answers to get pushed to the top. – Mark Booth Feb 02 '12 at 13:32

6 Answers6

87

As of PyDev 2.3.0, pep8 is integrated in PyDev by default, even shipping with a default version of it.

Open Window > Preferences

It must be enabled in PyDev > Editor > Code Analysis > pep8.py

Errors/Warnings should be shown as markers (as other things in the regular code analysis).

In the event a file is not analyzed, see https://stackoverflow.com/a/31001619/832230.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
  • 6
    It shows many warnings by default. However, when I run it from the command line, it shows even more. How can I convince it to show all the warnings in Eclipse as it is showing when run from the command line? Options are the same. Also, redirecting pep8 output to the console does nothing. – Krystian Cybulski Mar 01 '13 at 13:53
  • 2
    PyDev is currently using an older version because it uses its internal Jython version (which is currently at 2.2.1). There are plans to either make it run externally to get the results or updating the internal Jython to be able to support newer versions of pep8.py. – Fabio Zadrozny Feb 09 '14 at 10:24
  • Apparently, this is not functioning with PyDev 4.2.0, no markers are ever displayed, either in Warning or Error mode. – Luís de Sousa Jul 27 '15 at 11:50
  • 1
    Just tested it here and it does work for me... note that your sources must be under a source folder (i.e.: in the PYTHONPATH) for it to work (if it still doesn't, please report the issue at the pydev tracker). – Fabio Zadrozny Jul 27 '15 at 12:16
  • It started working once I set it to Error mode. There is also a delay from the moment you activate it until it starts marking the code. – Luís de Sousa Jul 28 '15 at 06:49
  • I found that it ignores the configured arguments unless you tell it to use the system interpreter. – Don Kirkby Oct 06 '15 at 22:08
26

I don't know how to integrate it for whole project, but I have used it as an external tool to analyze an individual file.

Note that the pycodestyle package is the official replacement for and is the newer version of the pep8 package. To install it, run:

$ sudo pip install --upgrade pycodestyle

Next, in Eclipse:

  1. Select Run-External Tools-External Tools Configurations...
  2. Select Program root node.
  3. Press New launch configuration button.
  4. Enter Name for your launch configuration. I use pycodestyle.
  5. Fill following fields:

    Location -- ${system_path:pycodestyle}

    Working directory -- ${container_loc}

    Arguments -- "${resource_name}" (This uses the currently active file.)

Go to Common tab and confirm that the Allocate Console checkbox is checked.

A benefit of this approach is that you can use a very up-to-date version of the package, and are not limited to the old version included with PyDev. And if you are curious about setting up pylint in a similar manner, see this answer.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Dmitry Kochkin
  • 923
  • 8
  • 17
  • 1
    If you're getting error _Variable references empty selection: ${container_loc}_ don't forget to add your source folder to PYTHONPATH (it might not be there if you import an existing project into workspace). **Right-click on project name > Properties > PyDev - PYTHONPATH > Source folders tab** and click **Add source folder**. Should be working like a charm. – berni May 18 '11 at 16:55
  • 2
    There's now a default pep8 integration. See: http://stackoverflow.com/a/8532188/110451 – Fabio Zadrozny Dec 16 '11 at 09:32
  • 1
    This is not as useful for pep8 CHECKING, as that is mostly available in Eclispe - but I found it great for autopep8! Thanks – tom stratton Jan 21 '13 at 15:22
12
  1. Open your Eclipse
  2. Go to Help and select Install New Software
  3. Click the Add button and a "Add Repository" Dialog box will appear
  4. You can use any name you like for it. (I used PyDev)
  5. For the location, enter "http://pydev.org/updates"
  6. Click Ok.
  7. You are now in the process of installation. Just wait for it to finish.
  8. After the installation, close Eclipse and Open it again.
  9. Now that PyDev is installed in your Eclipse, go to Window->Preferences
  10. Choose PyDev->Editor->Code Analysis
  11. Go to pep8.py tab
  12. Choose the radio button for warning and click Ok.

That's it. Your Eclipse IDE is now integrated with PEP8. To run pep8.py automatically, right click on your project editor. Choose PyDev and click "code analysis". In your problems tab in your workspace, you will see warnings that points to the line that you have made a violation in the PEP8 (if you have violated).

casperOne
  • 73,706
  • 19
  • 184
  • 253
Amazing Angelo
  • 1,658
  • 2
  • 13
  • 8
  • Something very strange, is that no matter how hard i tried to configure pep8, the warnings and info markers in the editor did not show up. That's until i added the "Problems" view, then it suddenly worked like a charm... – Agey Oct 17 '18 at 14:09
10

CODE ANALYSIS :

In Eclipse (PyDev), if you want to code analysis using pep8 style then

Go to:Windows -> Preferences -> PyDev -> Editor -> Code Analysis -> pep8.py tab and select Warning click Apply and OK button.

In your python code if you validate pep8 coding style it will give you warning

AUTO CODE FORMATTING :

In Eclipse (PyDev), if you want to Auto Format python code using pep8 style then

Go to:Windows -> Preferences -> PyDev -> Editor -> Code Style -> Code Formatter -> click on check-box (Use autopep8.py for console formatting?) click Apply and OK button.

If you want to increase length of line(pep8 default is 79) below Use autopep8.py you can set parameter type --max-line-length=150 if you set max length to 150

If press auto-format shortcut ( Ctrl + Shift + f ) it will automatically format your python code like pep8 style

Sagar Pise
  • 837
  • 10
  • 20
6

That does not yet appear to be fully integrated into Pydev.

As suggested in this post,

[it] would require changing the code within pydev -- a flexible option would be adding preferences to let the user choose to which patterns he wants to match for creating hyperlinks (and saying which group in the match is the line and which one is the file)...

Or, you can try it hard-coded playing with: org.python.pydev.debug.ui.PythonConsoleLineTracker (should be pretty easy to grasp).

A request does exist for just that, but it seems to be still open 1 year after its creation...

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-1

You don't :) Instead you take advantage of very good integration with PyLint and configure PyLint to check all things PEP8 checks. See How to configure PyLint to check all things PEP8 checks?

Community
  • 1
  • 1
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366