0

如果我选择以下代码并在eclipse中单击“运行选定的代码”,它会给我一个错误。

class abc(object):
    def __init__(self):
        print 'base'

    def run(self):
        print 'run'

错误信息:

class abc(object):
    def __init__(self):
        print 'base'

    def run(self):
        print 'run'
  File "<ipython-input-22-8e1ec627fd90>", line 1
    def run(self):
                  ^
SyntaxError: unexpected EOF while parsing

run

但是,如果我删除两个函数之间的空格,那么它将运行正常(见下文),这是 pydev 中的错误吗?有什么办法可以解决这个问题吗?

class abc(object):
    def __init__(self):
        print 'base'    
    def run(self):
        print 'run'

版本:
Eclipse 4.4.2
LiClipse 2.0.0.201504092214
Subclipse(必需)1.10.13

4

1 回答 1

1

I think what you're seeing is the result of using the interactive console to run your code (i.e.: http://www.pydev.org/manual_adv_interactive_console.html).

The issue is that when you send the code to the console through that action, it won't do any edit to your code, and when the console sees a line with 0-width, it'll conclude that the Python block is finished.

So, there are some workarounds for that:

  1. Don't right-trim your blocks (i.e.: leave spaces to the block indent instead of a 0-width line).

  2. If you don't want to run just a section of your code, deselect all the code and execute the whole file with Ctrl+Alt+Enter.

  3. Deselect the code, go to the first line and send the contents to the console line-by-line with F2 (F2 will send the current line and will move the cursor to the next line with contents and may even fix the indents, so, it should be easy to select the block you want to send to the console).

  4. Don't use the interactive console and do a plain run with F9 (although in this case as it'll launch in a new non-interactive console, the symbols won't be available for inspection afterwards).

  5. If you work using TDD (test driven development), then run the test code with Ctrl+F9 (see http://www.pydev.org/manual_adv_pyunit.html for details) -- again, in this mode it won't be using the interactive console.

于 2016-07-11T11:22:56.830 回答