我如何处理 UnresolvedImport Eclipse (Python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2451682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I handle an UnresolvedImport Eclipse (Python)
提问by Eric Wilson
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?
当我import MySQLdb
使用 PyDev 插件在 Eclipse 中编写时,我得到一个未解析的导入。但是,程序运行没有错误。我可以添加一个注释来消除错误,但是处理这个问题的正确方法是什么?
How can I help Eclipse know that MySQLdb is there?
我如何帮助 Eclipse 知道 MySQLdb 存在?
回答by cdleary
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.
听起来像是MySQLdb
在您的某个地方sys.path
,但不在您的 Eclipse 项目的PYTHONPATH
; 换句话说,Eclipse 认为您将在运行时遇到导入错误,因为您还没有完全配置它。Google 似乎说您可以更改此设置Window->Preferences->Preferences->PyDev->Python Interpreter
以包含MySQLdb
模块的路径。
For some help figuring out where MySQLdb
might be living on your system:
如需帮助确定MySQLdb
系统上可能存在的位置:
- 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.
- 打开交互式解释器,
import MySQLdb
- 如果成功的话,你可以从一个提示:
print MySQLdb.__file__
; 它可能是__init__
您需要将路径指向的包中的文件。
回答by PentheusLennuye
cdleary above provided the reason two years ago, but this may be easier. Basically, one reinstalls the interpreter.
上面的 cdleary 提供了两年前的原因,但这可能更容易。基本上,重新安装解释器。
- Select Window - > Preferences -> PyDev -> Interpreter - Python
- Select the python interpreter in the upper pane
- Click on Remove
- Click on Auto Config
- Agree to everything.
- 选择窗口 -> 首选项 -> PyDev -> 解释器 - Python
- 在上部窗格中选择 python 解释器
- 点击移除
- 点击自动配置
- 同意一切。
This works on Fedora 17 using the Eclipse 4.2.0 that came with the package management.
这适用于使用包管理附带的 Eclipse 4.2.0 的 Fedora 17。
回答by LG_PDX
Fixed this by doing two things:
通过做两件事来解决这个问题:
1) Added MySQLdb egg to the PYTHONPATH under Window->Preferences->Preferences->PyDev->Python Interpreter.
1) 在 Window->Preferences->Preferences->PyDev->Python Interpreter 下的 PYTHONPATH 中添加 MySQLdb egg。
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.
2) 关闭并重新打开带有红色 x 的 .py 文件。
回答by Apophenia Overload
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/
来源:http: //klaith.wordpress.com/2009/06/12/pydev-unresolved-import-errors/
回答by javasqlsecurity dot com
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
这肯定有效,我只是用 Pmw 包试过了。在站点包中解压缩包。然后从 eclipse 中删除 python 解释器,然后再次添加它。您的导入错误将消失。您也可能希望将模块添加到强制内置函数中。请参阅如何修复 PyDev“未定义的导入变量”错误?和http://pydev.org/manual_101_interpreter.html
回答by Barthelemy
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.
我曾经在 Windows 上遇到过类似的问题(尽管从未在 Linux 上遇到过这个问题),我发现我必须将我的库的 .egg 目录包含到我的 PYTHONPATH 中。
For example my PYTHONPATH (Pydev/Interpreter - Python/Libraries) included:
例如我的 PYTHONPATH (Pydev/Interpreter - Python/Libraries) 包括:
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.
使用 jinja。
回答by James Oravec
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.
我遇到了类似的问题,以下是我为解决问题所做的工作。我有一台 Windows 8 机器,安装了 Python 2.7 并通过 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.
当我执行easy install
它时,它尝试安装MySQL-python 1.2.5
失败并带有error: Unable to find vcvarsall.bat
. 我做了easy_install
的pip
,并试图pip
安装也失败,类似的错误。他们都参考vcvarsall.bat
了与visual studio有关的东西,因为我的机器上没有visual studio,这让我寻找不同的解决方案,我在下面分享。
The Solution:
解决方案:
- Reinstall python 2.7.8 from 2.7.8 from https://www.python.org/downloadthis 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
- 从https://www.python.org/download从 2.7.8 重新安装 python 2.7.8这将添加任何丢失的注册表设置,这是下次安装所需的。
- 从http://pypi.python.org/pypi/MySQL-python/1.2.4安装 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.
在我完成这两个安装之后,我重新打开了 eclipse 并得到了更新我接受的 eclipse 路径的提示,之后我就可以查询我的 MySQL 数据库了。
回答by intoo
import MySQLdb
If this code show error like this:
如果此代码显示如下错误:
Unresolved import: MySQLdb
未解析的导入:MySQLdb
you should add D:\Python27\Lib\site-packages\MySQLdb
to your sys.path
.
你应该添加D:\Python27\Lib\site-packages\MySQLdb
到你的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.
D:\Python27\Lib\site-packages\MySQLdb
这是您MySQLdb
在计算机磁盘中安装的位置。执行此步骤后,错误将消失。