python 如何让 PyDev 编辑器有选择地忽略错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1702043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:53:16  来源:igfitidea点击:

How can I make the PyDev editor selectively ignore errors?

pythonpydevjythonpython-import

提问by Pridkett

I'm using PyDev under Eclipse to write some Jython code. I've got numerous instances where I need to do something like this:

我在 Eclipse 下使用 PyDev 编写一些 Jython 代码。我有很多实例需要做这样的事情:

import com.work.project.component.client.Interface.ISubInterface as ISubInterface

The problem is that PyDev will always flag this as an error and say "Unresolved import: ISubInterface". The code works just fine, it's just that I'd rather not have these little white/red X-marks next to my code and have my Problems tab littered with these errors.

问题是 PyDev 将始终将此标记为错误并说“未解析的导入:ISubInterface”。代码工作得很好,只是我不想在我的代码旁边有这些白色/红色的小 X 标记,并且我的问题选项卡上到处都是这些错误。

Is there a way I can add a magic comment or something like that to the end of the line to make PyDev ignore the false error, similar to how you can sprinkle comments like "# pylint: disable-msg=E1101" to make PyLint ignore errors?

有没有办法我可以在行尾添加一个神奇的注释或类似的东西来让 PyDev 忽略错误的错误,类似于你如何可以撒上诸如“# pylint: disable-msg=E1101”之类的注释来让 PyLint 忽略错误?

Also, there's a possibility I'm just doing it wrong when it comes to using Java interfaces in Jython. In which case a little bit of guidance would be very much appreciated.

此外,在 Jython 中使用 Java 接口时,我有可能只是做错了。在这种情况下,我们将非常感谢您的指导。

回答by deets

You can add a comment

您可以添加评论

#@UnresolvedImport
#@UnusedVariable

So your import becomes:

所以你的导入变成:

import com.work.project.component.client.Interface.ISubInterface as ISubInterface #@UnresolvedImport

That should remove the error/warning. There are other comments you can add as well.

那应该消除错误/警告。您还可以添加其他评论。

回答by Kevin

Add the hash character # at the end of the line then with the cursor on the flagged error, press Ctrl-1. One of the options in the menu will be something like @UndefinedVariable. Adding this comment will cause PyDev to ignore the error.

在行尾添加井号 #,然后将光标放在标记的错误上,按 Ctrl-1。菜单中的选项之一类似于@UndefinedVariable。添加此注释将导致 PyDev 忽略该错误。

回答by Fabio Zadrozny

You can make the ignore like the other posts suggest, but the real problem is that Pydev cannot find that class... If you add a .jar that contains that class to your PYTHONPATH it should be able to resolve it (or if you have a Java project that has that class, you should be able to mark that project as a Pydev project and add its bin folder to the project PYTHONPATH -- in which case that class should be found too).

你可以像其他帖子建议的那样忽略,但真正的问题是 Pydev 找不到那个类......如果你添加一个包含该类的 .jar 到你的 PYTHONPATH 它应该能够解决它(或者如果你有具有该类的 Java 项目,您应该能够将该项目标记为 Pydev 项目并将其 bin 文件夹添加到项目 PYTHONPATH - 在这种情况下也应该找到该类)。

回答by Tom Green

It is not a PYTHONPATH issue. It is related to importing/using static class-internal members of a Java class. I am getting the same sort of thing all over the place e.g. when trying to use constants in java.awt.Color:

这不是 PYTHONPATH 问题。它与导入/使用 Java 类的静态类内部成员有关。我在所有地方都遇到了同样的事情,例如在尝试在 java.awt.Color 中使用常量时:

import java.awt.Color as Color
borderColor = Color.BLACK # get "Undefined variable from import: BLACK" error

There is no way I've found to import Color.BLACK in this case. Thanks to iceman for at least pointing out the #@UndefinedVariable flag. That helps a lot. Note also that this is NOT a jython problem, the code runs just fine. It's just an issue with PyDev.

在这种情况下,我找不到导入 Color.BLACK 的方法。感谢 iceman 至少指出了 #@UndefinedVariable 标志。这很有帮助。还要注意,这不是 jython 问题,代码运行得很好。这只是 PyDev 的一个问题。