140

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?

1
  • I didn't see anything related to pylint in these answers :\ I think that's where this notice originates from and I was hoping to find how to tell pylint that that's no variable, its an import. Feb 1, 2015 at 22:46

13 Answers 13

158

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 http://pydev.org/manual_101_interpreter.html 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 (http://pydev.org/manual_101_interpreter.html also has details on that).

12
  • 1
    sometimes adding a module to forced builtins doesn't help (see this bug, for example sourceforge.net/tracker/…) Nov 7, 2010 at 8:35
  • @Fabio: What about predefined completions could they be used here? And what options are there if you do something like try: import a; except: a=mockModule is there any Way to suppress these without the comments?
    – ted
    Oct 22, 2012 at 13:10
  • 9
    I tried Ctrl-1 it gives me '@UndefinedVariable' which adds an anotation and that works. Thanks. Jan 3, 2013 at 21:25
  • I'm experiencing the same issue with latest Eclipse and PyDev. I can navigate between all modules, so why code-analysis can't do the same?
    – Adam
    Sep 3, 2014 at 11:41
  • 4
    Before you go to extremes, make sure the directory is in PYTHONPATH, then quit/restart Eclipse, do Project>Pydev>Remove error markers, and numpy should now appear in the Forced builtins.
    – smci
    Nov 1, 2017 at 16:43
50

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

2
  • 9
    Thanks for this. It is simple and easy. However, instead of changing the "Import not found" setting to "Ignore", I changed it to "Warning". NOW NOTHING SHOWS UP. :) Hopefully, I'll get a Warning if I have a REAL import problem. Aug 24, 2012 at 19:45
  • 1
    Had to restart Eclipse for the changes to take effect.
    – spettekaka
    Sep 12, 2018 at 9:19
28

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.

4
  • 1
    Considering that the "post marked as answer" was written by the developer of PyDev, you should perhaps take another look at it. He doesn't give explicit instructions in the post itself because he links to the instructions you specified. Jul 23, 2015 at 18:30
  • 5
    @CoreDumpError I don't care who wrote a post. I tried what the post describes, and it works, but is a workaround, not a solution. What I describe might not work for other computers, but works for me and is the correct solution. If the other post links to the solution instead of describing it, then it does not follow the guidelines, I followed the guidelines by describing what is a good solution for me, hoping that will help others.
    – stenci
    Jul 23, 2015 at 20:18
  • 3
    @Isaac Now it does, I posted my answer before it was edited.
    – stenci
    Oct 24, 2017 at 15:52
  • Yes, this is much better and cleaner solution! Instead of masking or hiding it offers the legit fix. I had to add re but once I did no more red ink for things like re.M
    – Bostone
    Jun 17, 2019 at 21:30
10

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.

2
  • Just wanted to provide verification that this in fact works perfectly.
    – Amndeep7
    Jan 22, 2013 at 15:13
  • Yep, simply went to main menu Project/Properties/PyDev - PYTHONPATH and added all the paths needed for the app, which solved the issue !
    – Kostyantyn
    Jul 2, 2013 at 21:42
7

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.

1
  • worked for me too, thanks for making each step more explicit than in the currently accepted answer.
    – sc28
    Apr 16, 2019 at 13:58
3

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

2
  • 5
    This is Eclipse's favorite pastime. Making error flags suddenly appear for no reason. It drives people crazy thinking that they broke THE WORLD, except it's some nonsense error flag that Eclipse has suddenly decided to implement randomly for fun and profit(?)
    – R Thiede
    Jun 8, 2012 at 8:48
  • 1
    I tried this, and thought it had worked, but it was only a ruse, but it took > 5min, so I can't undo. However @Fabio Zadrozny ctrl-1 @UndefinedVariable worked perfectly, and it continues to work even after 5 minutes! May 11, 2013 at 7:52
1

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.

0

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

0

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.

0

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

0

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.

0

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.

1
  • How can i add lib.site-packages._pytest to forced builtins... it's not accepting '-' symbol... I'm having import issue with config module and trying to do your remedy, please help
    – zeal
    Jul 25, 2016 at 9:34
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.