Fix Python – How do I fix PyDev “Undefined variable from import” errors?


Asked By – Chris B.

I’ve got a Python project using PyDev in Eclipse, and PyDev keeps generating false errors for my code. I have a module settings that defines a settings object. I import that in module b and assign an attribute with:

from settings import settings
settings.main = object()

In some of my code–but not all of it, statements like:

from settings import settings
print settings.main 

… generate “Undefined variable from import: main” messages in the Eclipse code error pane, even though the code runs without a problem. How can I correct these?

Now we will see solution for issue: How do I fix PyDev “Undefined variable from import” errors?


For code in your project, the only way is adding a declaration saying that you expected that — possibly protected by an if False so that it doesn’t execute (the static code-analysis only sees what you see, not runtime info — if you opened that module yourself, you’d have no indication that main was expected).

To overcome this there are some choices:

  1. If it is some external module, it’s possible to add it to the forced builtins so that PyDev spawns a shell for it to obtain runtime information (see for details) — i.e.: mostly, PyDev will import the module in a shell and do a dir(module) and dir on the classes found in the module to present completions and make code analysis.

  2. You can use Ctrl+1 (Cmd+1 for Mac) in a line with an error and PyDev will present you an option to add a comment to ignore that error.

  3. It’s possible to create a stub module and add it to the predefined completions ( also has details on that).

This question is answered By – Fabio Zadrozny

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0