1

I'm using eclipse/aptana for python development. I have no issue with my python path config, autocomplete works fine with everything, however I am having the following problem:

When I have a "constant" in one of my classes and I try to access it, eclipse complains about "undefined variable from import" on the constant, eg

class Universe(object):
    ULTIMATE_ANSWER = 42

# in different module
# edit from: import Universe
from bigbang.models import Universe
print Universe.ULTIMATE_ANSWER

where Universe.ULTIMATE_ANSWER triggers the warning. Additionally, autocomplete works fine, so when I type Universe.x I do get all the constants proposed.

It's not an urgent issue, however it tends to become annoying, and might make you ignore actual errors.

Any idea on how to make eclipse behave on this one? :)

Edit: This only happens when importing the class in a another module.

Edit 2: In case it's not clear above, the code works, this is just about the warning that shouldn't be there... I have tried and replicated this on projects other than mine, both in eclipse and aptana with pydev.

Edit 3: As with the comments bellow, this is probably a bug in pydev. Submitted and waiting...

Geekfish
  • 2,173
  • 23
  • 28
  • I think this might be a bug :) – Mark Sep 13 '11 at 21:14
  • already reported, will probably be lost between the millions of fake import error bugs :'( – Geekfish Sep 13 '11 at 21:16
  • Is it possible it's not recreating the content assist data? It should recreate when you hit save on each file. Or do you have some strange modules included by default? – Mark Sep 13 '11 at 23:10
  • The issue is too specific unfortunately, it only affects class constants. Autocomplete working also means the code assistant is aware of the module and its contents. – Geekfish Sep 14 '11 at 08:59
  • Additionally, instantiating the class and then referring to the constant as an object variable does not trigger the same warning – Geekfish Sep 14 '11 at 09:00

2 Answers2

1

if, (see @Aix) you meant from mymodel import Universe:

Use ctrl+1 after Universe.ULTIMATE_ANSWER; You will be asked to add a comment to ignore that error.

You can also add your model as a forced builtin, prompting analyzis as in runtime (which you said, gave no error)

Remi
  • 20,619
  • 8
  • 57
  • 41
  • This is a workaround proposed for cases where code analysis cannot be easily done, like when you're using your own import functions. However workaround is not a solution, and I would have to do it on every singly constant. – Geekfish Sep 13 '11 at 20:57
  • It looks like [this](http://stackoverflow.com/questions/2112715/how-do-i-fix-pydev-undefined-variable-from-import-errors) on which @Fabio Zadrozny proposed the workaround. I see you are in [contact with him](http://sourceforge.net/tracker/?func=detail&aid=3408721&group_id=85796&atid=577329) on SourceForge; well done! Keep us informed! – Remi Sep 15 '11 at 15:46
  • looks similar, but after trying to replicate it and share the exact code, I found that it probably has to do with something on the project structure. I will investigate some more, then submit the findings to the bug report, then maybe do the workaround :) – Geekfish Sep 16 '11 at 10:09
0

You don't import a class, you import a module.

If your module is called Universe (as is your class), then the fully qualified name of the variable is Universe.Universe.ULTIMATE_ANSWER.

If you defined the variable at the top level of the module (i.e. outside your class), then it would be called Universe.ULTIMATE_ANSWER:

# Universe module
ULTIMATE_ANSWER = 42
class Universe(object):
    pass

# in different module
import Universe
print Universe.ULTIMATE_ANSWER
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I know how to import modules, I wrote it like that for the shake of simplicity. The code works, the autocomplete works, all other code analysis work. Universe is just an example :) – Geekfish Sep 13 '11 at 20:54