Aptana Studio and “Undefined variable from import: DoesNotExist”

aptana-undefined-variableIn my journey from Java hacker to a Django developer, I’ve test driven a bunch of Python IDEs (ranging from true dev tools like Aptana, Eclipse/PyDev, PyCharm to simple editors like Notepad++).

Recent I was adding some django-profile code I found via Scot Hacker’s very helpful “django-profiles: The Missing Manual“.

When I added this bit of code for a custom form object:

    def __init__(self, *args, **kwargs):
        super(ProfileForm, self).__init__(*args, **kwargs)
        try:
            self.fields['email'].initial = self.instance.user.email
        except User.DoesNotExist:
            pass

Aptana would call out “DoesNotExist” with the error: “Undefined variable from import: DoesNotExist”. Thanks to Google and the DjangoBot I learned that this is because “DoesNotExist” is added by the metaclass.

Here’s the fix

  1. Open up Aptana Studio (I’m v2.0.3 btw)
  2. Open Window > Preferences > PyDev > Editor > Code Analysis
  3. Select the “Undefined” tab
  4. Add DoesNotExist at the end of the “Consider the following names as globals” list
  5. Apply and restart

Error gone. In the immortal words of ubernostrum: “Bah. It’s not like metaclasses are *that* hard to statically figure out.” I’ll take your word for that, James.