views:

469

answers:

4

When I write import MySQLdb in Eclipse using the PyDev plugin, I get an unresolved import. However, the program runs without error. I can add an annotation to get the error to go away, but what is the right way to handle this?

How can I help Eclipse know that MySQLdb is there?

+1  A: 

It sounds like MySQLdb is somewhere on your sys.path, but not on your Eclipse project's PYTHONPATH; in other words, Eclipse thinks you're going to get an import error at runtime because you haven't fully configured it. Google seems to say that you can alter this setting in Window->Preferences->Preferences->PyDev->Python Interpreter to include the path to your MySQLdb module.

For some help figuring out where MySQLdb might be living on your system:

  1. Open an interactive interpreter,
  2. import MySQLdb
  3. If that succeeds, you can get a hint from: print MySQLdb.__file__; it may be the __init__ file in the package that you need to point the path at.
cdleary
Hmm . . . sounds great, followed instructions, it all seemed so likely. But the error remains.
FarmBoy
Not to sound too cliche, but did you try turning Eclipse off and on again afterwards? Other than that, I'm all out of ideas.
cdleary
I think I did. I'll also try to clean the project.
FarmBoy
A: 

I once had a similar problem on Windows (never encountered this on Linux though) and I discovered that I had to include the .egg directory of my library to my PYTHONPATH.

For example my PYTHONPATH (Pydev/Interpreter - Python/Libraries) included:

C:\Python26\Lib\site-packages

and I had to add:

C:\Python26\Lib\site-packages\jinja2-2.2.1-py2.6.egg

to use jinja.

Barthelemy
Thanks, but that didn't work either.
FarmBoy
A: 

Fixed this by doing two things:

1) Added MySQLdb egg to the PYTHONPATH under Window->Preferences->Preferences->PyDev->Python Interpreter.

C:\Python26\Lib\site-packages\MySQL_python-1.2.3c1-py2.6-win32.egg

2) Close and re-open the .py file that had the red x.

NW Architect
A: 

Adding the egg works, but the error remains. The solution for that error can be found by adding

#@UnresolvedImport

To the import statement, as in:

import web #@UnresolvedImport

Source: http://klaith.wordpress.com/2009/06/12/pydev-unresolved-import-errors/

Apophenia Overload