如何在 Eclipse 中集成 pep8.py?

一个小小的背景:

  • PEP 8 Python 代码样式指南,它包含了所有 Python 程序员应该遵循的约定。
  • Pep8.py 是一个(非常有用的)脚本,它检查给定 Python 脚本的代码格式。
  • Eclipse 是一个很棒的 IDE,通过 皮德夫扩展,它可以用来开发 Python

在编写脚本时,我手动运行 pep8.py,但对于更大的项目,我更喜欢使用 Eclipse。 在 Eclipse/Pydev 中集成 pep8.py 将非常有用,这样它就可以在项目中的所有文件中自动运行,并指向包含警告的行。 也许有一个显而易见的方法可以做到这一点,但我还没有找到它。

问题是: 如何在 Eclipse 中集成 pep8.py?

35618 次浏览

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...

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.

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?

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.

  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).

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