11

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?

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270

8 Answers8

10

cdleary above provided the reason two years ago, but this may be easier. Basically, one reinstalls the interpreter.

  1. Select Window - > Preferences -> PyDev -> Interpreter - Python
  2. Select the python interpreter in the upper pane
  3. Click on Remove
  4. Click on Auto Config
  5. Agree to everything.

This works on Fedora 17 using the Eclipse 4.2.0 that came with the package management.

PentheusLennuye
  • 319
  • 2
  • 7
10

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
  • 69,512
  • 53
  • 163
  • 191
  • Hmm . . . sounds great, followed instructions, it all seemed so likely. But the error remains. – Eric Wilson Mar 17 '10 at 01:09
  • 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 Mar 17 '10 at 07:15
  • I think I did. I'll also try to clean the project. – Eric Wilson Mar 17 '10 at 16:00
  • 1
    For other searchers, I wanted to add my experience -Windows 7, Python 2.7.1, Eclipse 3.6.2, PyDev, Pyschopg (For Python 2.7 amd64). The install of Psychopg went to C:\Python27\Lib\site-packages\psycopg2 ( under the Python 2.7 installation ). I first referenced that folder and all child folders with __init__.py, but found I had to reference the site_packages folder as well. ( http://stackoverflow.com/questions/2983088/unresolved-import-models ) ( http://stackoverflow.com/questions/4631377/unresolved-import-issues-with-pydev-and-eclipse ) – Jeff Maass Apr 22 '11 at 13:21
6

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.

LG_PDX
  • 834
  • 8
  • 12
2

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
  • 2,485
  • 3
  • 28
  • 46
  • I read the article and tried this. The original code did an import lxml.etree as etree and then instantiated etree.HTMLParser(). I changed this to lxml.etree.HTMLParser() and still got the error. I tried adding import lxml first. I included the phrase above. That didn't help. When the file is closed, the Problems list shows no errors they magically disappear. When I opened the .py file it shows the errors again. – RichMeister Jan 03 '13 at 21:18
0

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
  • 8,277
  • 6
  • 33
  • 36
0

I had a similar issue and the following is what I did to solve my issue. I have a Windows 8 Machine, Python 2.7 installed and running my stuff through eclipse.

Some Background:

When I did an easy install it tries to install MySQL-python 1.2.5 which failed with an error: Unable to find vcvarsall.bat. I did an easy_install of pip and tried the pip install which also failed with a similar error. They both reference vcvarsall.bat which is something to do with visual studio, since I don't have visual studio on my machine, it left me looking for a different solution, which I share below.

The Solution:

  1. Reinstall python 2.7.8 from 2.7.8 from https://www.python.org/download this will add any missing registry settings, which is required by the next install.
  2. Install 1.2.4 from http://pypi.python.org/pypi/MySQL-python/1.2.4

After I did both of those installs, I reopened eclipse and got a prompt to update the paths of eclipse which I accepted, after that I was able to query my MySQL db.

James Oravec
  • 19,579
  • 27
  • 94
  • 160
0
import MySQLdb

If this code show error like this:

Unresolved import: MySQLdb

you should add D:\Python27\Lib\site-packages\MySQLdb to your sys.path.

D:\Python27\Lib\site-packages\MySQLdb is this location where you install MySQLdb in your computer disk. After this step, the error will disappear.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
intoo
  • 87
  • 4
  • I think Eclipse is a good tool and is convenience. You can use [JetBrains PyCharm 4.0.4](https://www.jetbrains.com/). Are you use it? – intoo Dec 15 '15 at 11:18
0

This surely works I just tried it with Pmw package. Unzip package in site-packages. Then remove python interpreter from eclipse and then add it again. Your import errors shall go away. also you may want add module to forced builtins. See How do I fix PyDev "Undefined variable from import" errors? and http://pydev.org/manual_101_interpreter.html

Community
  • 1
  • 1