[python] How do I fix PyDev "Undefined variable from import" errors?

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?

This question is related to python code-analysis pydev

The answer is


I had the same problem. I am using Python and Eclipse on Windows. The code was running just fine, but eclipse show errors everywhere. After I changed the name of the folder 'Lib' to 'lib' (C:\Python27\lib), the problem was solved. It seems that if the capitalization of the letters doesn't match the one in the configuration file, this will sometimes cause problems (but it seems like not always, because the error checking was fine for long time before the problems suddenly appeared for no obvious reason).


The post marked as answer gives a workaround, not a solution.

This solution works for me:

  • Go to Window - Preferences - PyDev - Interpreters - Python Interpreter
  • Go to the Forced builtins tab
  • Click on New...
  • Type the name of the module (multiprocessing in my case) and click OK

Not only will the error messages disappear, the module members will also be recognized.


I find that these 2 steps work for me all the time:

  1. Confirm (else add) the parent folder of the module to the PYTHONPATH.
  2. Add FULL name of the module to forced builtins.

The things to note here:

  • Some popular modules install with some parent and child pair having the same name. In these cases you also have to add that parent to PYTHONPATH, in addition to its grandparent folder, which you already confirmed/added for everything else.

  • Use (for example) "google.appengine.api.memcache" when adding to forced builtins, NOT "memcache" only, where "google" in this example, is an immediate child of a folder defined in PYTHONPATH.


in preferences --> PyDev --> PyLint under arguments to pass to PyLint add this line:

--generated-members=objects

you will need to do this for each generated . I found this by googling, but I lost the reference.


This worked for me:

step 1) Removing the interpreter, auto configuring it again

step 2) Window - Preferences - PyDev - Interpreters - Python Interpreter Go to the Forced builtins tab Click on New... Type the name of the module (curses in my case) and click OK

step 3) Right click in the project explorer on whichever module is giving errors. Go to PyDev->Code analysis.


Right click in the project explorer on whichever module is giving errors. Go to PyDev->Remove Error Markers.


An approximation of what I was doing:

import module.submodule

class MyClass:
    constant = submodule.constant

To which pylint said: E: 4,15: Undefined variable 'submodule' (undefined-variable)

I resolved this by changing my import like:

from module.submodule import CONSTANT

class MyClass:
    constant = CONSTANT

Note: I also renamed by imported variable to have an uppercase name to reflect its constant nature.


My answer doesn't contribute anything new, just a concrete example I encountered.

import gtk.gdk

w = gtk.gdk.get_default_root_window()

PyDev showed the error message "Undefined variable from import: get_default_root_window()"

In the python shell you can see that this is a 'built-in' module as mentioned in a answer above:

>>> import gtk.gdk
>>> gtk.gdk
<module 'gtk.gdk' (built-in)>

Now under Window->Preferences->PyDev->Interpreters->Python Interpreter, I selected the tab 'Forced Builtins' and added 'gtk.gdk' to the list.

Now the error message didn't show anymore.


I'm using opencv which relies on binaries etc so I have scripts where every other line has this silly error. Python is a dynamic language so such occasions shouldn't be considered errors.

I removed these errors altogether by going to:

Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Undefined -> Undefined Variable From Import -> Ignore

And that's that.

It may also be, Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Imports -> Import not found -> Ignore


I was having a similar problem with an Eclipse/PyDev project. In this project the root directory of the python code was a sub-directory of the project.

--> MyProject
 + --> src         Root of python code
   + --> module1     A module 
   + --> module2     Another module
 + --> docs
 + --> test

When the project was debugged or run everything was fine as the working directory was set to the correct place. However the PyDev code analysis was failing to find any imports from module1 or module2.

Solution was to edit the project properties -> PyDev - PYTHONPATH section and remove /MyProject from the source folders tab and add /MyProject/src to it instead.


If you're sure that your script runs and that it is a false alarm, Go to Preferences > PyDev > Editor > Code Analysis. Demote the errors to warnings.

enter image description here

http://www.pydev.org/manual_adv_code_analysis.html


It is possible you just need to re-configure your python path within Eclipse. See my answer to a similar question.