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 开发者_如何学JAVAan 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?
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:
- Open an interactive interpreter,
import MySQLdb
- 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 above provided the reason two years ago, but this may be easier. Basically, one reinstalls the interpreter.
- Select Window - > Preferences -> PyDev -> Interpreter - Python
- Select the python interpreter in the upper pane
- Click on Remove
- Click on Auto Config
- Agree to everything.
This works on Fedora 17 using the Eclipse 4.2.0 that came with the package management.
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.
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/
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.
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
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:
- 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.
- 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.
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.
精彩评论