34

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

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.

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?

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.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Pridkett
  • 4,883
  • 4
  • 30
  • 47
  • You can make Pylint ignore errors but the error is from Pydev here and I don't know how to suppress this. – mmmmmm Nov 09 '09 at 17:18

4 Answers4

57

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.

David Hall
  • 32,624
  • 10
  • 90
  • 127
deets
  • 6,285
  • 29
  • 28
30

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.

Kevin
  • 8,353
  • 3
  • 37
  • 33
  • This doesn't seem to work, at least in Eclipse Juno, latest PyDev. Autofix shows the option for `@UndefinedVariable`, but choosing it has no effect. – Jeff Axelrod Oct 13 '12 at 18:51
  • (little thing: Cmd-1 on Mac; and you need to have the # already added otherwise the @UndefinedVariable will appear as an option but never add) – Michael Scott Asato Cuthbert Jan 12 '13 at 01:44
  • 1
    Works for me! Thank God! Got rid of those annoying "broken package" icons when things like win-curses or Celery added variables at runtime – std''OrgnlDave Aug 27 '16 at 14:52
  • 1
    This was very useful. It was frustrating to see red X errors where I know there is not a problem as the code works fine. I have not been able to fine a site that list all the available `#@` options. Do you have a link for the list of options? – Mike - SMT May 18 '17 at 14:48
  • 1
    Thanks for explaining the ctrl-1 feature. Very useful. – shrewmouse Jul 31 '18 at 13:16
6

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

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
-1

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:

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.

  • That was stated as much in the original question: "The problem is that PyDev will always flag this as an error and say "Unresolved import: ISubInterface". The code works just fine" – Pridkett Jun 18 '10 at 14:51