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?

Chris B.
  • 85,731
  • 25
  • 98
  • 139
  • 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. – ThorSummoner Feb 01 '15 at 22:46

13 Answers13

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

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
  • 1
    sometimes adding a module to forced builtins doesn't help (see this bug, for example http://sourceforge.net/tracker/?func=detail&atid=577329&aid=3090346&group_id=85796) – Boris Gorelik Nov 07 '10 at 08:35
  • @Fabio: What about [predefined completions](http://pydev.org/manual_101_interpreter.html#PyDevInterpreterConfiguration-PredefinedCompletions) 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 '12 at 13:10
  • 9
    I tried Ctrl-1 it gives me '@UndefinedVariable' which adds an anotation and that works. Thanks. – RichMeister Jan 03 '13 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 03 '14 at 11:41
  • Hi Adam, it may depend on your particular use-case... if you feel it should find it, please create an issue at https://sw-brainwy.rhcloud.com/tracker/PyDev/ – Fabio Zadrozny Sep 03 '14 at 13:07
  • I'm pressing ctrl+1, everywhere, cursor start of line, line highlighted, end of line, somewhere in the line.... nothing, no response. This seems to be common for me, is this the normal behavior of Eclipse, acting weird not working. P.S. The Pydev editor minimizes at mouse click(inside editor) or action randomly. – somethingSomething Feb 01 '15 at 21:05
  • Well, I've never seen that happen... please report that as an issue at https://sw-brainwy.rhcloud.com/tracker/PyDev/ (note that my first guess here is that you have a hardware issue with your mouse or keyboard). Please include details on your installation/OS... – Fabio Zadrozny Feb 02 '15 at 12:20
  • I got this issue with compiled `re` module regexes that were defined in one module, but imported by another module that uses its `sub()` method. Eclipse shows an `Undefined variable from import: sub` error. The `Ctrl+1` to add `# '@UndefinedVariable'` did the trick for me. +1 – CivFan Jul 29 '15 at 20:50
  • I think @SJP has a better answer. You make one change and it works for all you code instead of adding a comment for each infraction. – Prof Mo Aug 23 '17 at 11:36
  • @ProfMo -- actually, his answer is included in mine. From the 3 choices available it's the second one I comment -- maybe I should reorder so that it's the first one ;) – Fabio Zadrozny Aug 23 '17 at 15:21
  • 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 01 '17 at 16:43
  • I had the same issue. It was telling me it couldn't resolve `auto` from the line `from enum import Enum, auto`. Adding enum as a forced builtin fixed the issue for me. – Josh Davis Sep 15 '21 at 14:35
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

rozap
  • 621
  • 1
  • 4
  • 13
ubershmekel
  • 11,864
  • 10
  • 72
  • 89
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.

stenci
  • 8,290
  • 14
  • 64
  • 104
  • 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. – coredumperror Jul 23 '15 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 '15 at 20:18
  • 3
    @Isaac Now it does, I posted my answer before it was edited. – stenci Oct 24 '17 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 '19 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.

Jules
  • 563
  • 1
  • 5
  • 16
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.

SJP
  • 221
  • 3
  • 6
  • worked for me too, thanks for making each step more explicit than in the currently accepted answer. – sc28 Apr 16 '19 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).

Leon
  • 39
  • 1
  • 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 08 '12 at 08:48
  • 1
    I tried this, and thought it had worked, but it was only a [ruse](https://www.google.com/search?q=define:ruse), but it took > 5min, so I can't undo. However @[Fabio Zadrozny](http://stackoverflow.com/users/110451/fabio-zadrozny) ctrl-1 @UndefinedVariable worked perfectly, and it continues to work even after 5 minutes! – Mark Mikofski May 11 '13 at 07: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.

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
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.

Mark Mikofski
  • 19,398
  • 2
  • 57
  • 90
0

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

Chet
  • 1,209
  • 1
  • 11
  • 29
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.

chriad
  • 1,392
  • 15
  • 22
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.

Aikude
  • 607
  • 7
  • 8
  • 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 '16 at 09:34
0

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

Community
  • 1
  • 1
oob
  • 1,948
  • 3
  • 26
  • 48
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