eclipse pydev 中未解决的导入 [英] unresolved import in eclipse pydev
问题描述
我刚刚为 python 正确安装了 google gdata 库(脚本实际上运行良好).我正在 Eclipse 中使用 pydev.我导入了一个模块,但导入命令仍然是红色的卷曲下划线,如屏幕截图所示.
I just properly installed the google gdata library for python (the script actually runs fine). I'm working with pydev in eclipse. I imported a module but the import command remains curly red underlined as you can see on the screenshot.
我添加了以下和 ProjectName->Properties,但它不起作用(尽管路径绝对正确):
I added the following und ProjectName->Properties, but it does not work (although the path is definitly correct):
我必须做什么才能使 Eclipse 正确解析导入?
What do I have to do such that the import is resovled correctly by eclipse?
推荐答案
如果你有这样的 import 语句:import gdata.spreadsheet.service
那么你需要确保在 PYTHONPATH
有一个包含 gdata
子目录的目录(并且 gdata
应该有 spreadsheet
子目录和 service.py
> 模块文件).
If you have import statement like this: import gdata.spreadsheet.service
then you need to make sure that on PYTHONPATH
there is a directory that contains gdata
subdirectory (and gdata
should have spreadsheet
subdirectory with service.py
module file).
在你的情况下:如果你的 gdata
目录在 python 发行版的 site-packages
目录中,那么你需要确保 site-packages
code> 在 PYTHONPATH 上(不是你试图放在那里的 site-packages/gdata/spreadsheet
).
In your case: if your gdata
directory is in site-packages
directory on python distribution, then you need to make sure that site-packages
is on PYTHONPATH (not site-packages/gdata/spreadsheet
that you were trying to put there).
查看http://docs.python.org/2/tutorial/modules.html在6.4 包"部分中作为示例和参考.
Look at http://docs.python.org/2/tutorial/modules.html in "6.4 Packages" section for example and reference.
在 PyDev 中你可以在两个地方修改 PYTHONPATH 的内容:
In PyDev you can modify the contents of PYTHONPATH in two places:
- 窗口 -> PyDev -> 解释器 - Python
- 在您使用的窗口中
我建议您首先检查 site-packages
目录是否在您的 Window ->PyDev ->解释器 - Python 设置 ->系统 PYTHONPATH
.默认情况下它应该在那里,所以如果你没有改变它,它应该在那里(这可能就是你的应用程序工作的原因!).
I would suggest you to first check that site-packages
directory is in your Window -> PyDev -> Interpreter - Python settings -> System PYTHONPATH
. It should be there by default, so if you didn't change it, it should be present there (and probably that's why your application works!).
现在,由于某些原因,PyDev 在将新库添加到 site-packages
后经常会在刷新"有关可用库的信息时遇到问题(例如,它使用该信息来检查它是否应该在导入语句上放置错误标记).
Now, from some reasons, PyDev often has problems with "refreshing" info about available libraries after adding a new library to site-packages
(and it uses that info for example to check if it should put error marker on import statement).
有几种方法可以强制刷新此信息.对我来说最可靠的是通过以下方式删除python解释器:Window ->PyDev ->解释器 - Python ->删除
,然后将其添加回同一视图中.然后重新扫描 site-packages
目录,PyDev 会看到更新的库集,因此错误标记应该消失.
There are a few ways to force the refresh of this info. The most reliable for me is just removing python interpreter by means of: Window -> PyDev -> Interpreter - Python -> Remove
and then adding it back in the same view. Then the site-packages
directory is rescanned and PyDev sees the updated set of libraries, so the error markers should disappear.
旁注:老实说,到目前为止我还不需要使用这个 External Libraries
视图.在解释器设置中包含站点包对于 99% 的情况来说已经足够了,因为应该放置 Python 外部库.
Side note: To be honest, so far I haven't ever need to use this External Libraries
view. Having site-packages in Interpreter settings is fine enought for 99% of cases because there is where Python external libraries should be put.
这篇关于eclipse pydev 中未解决的导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!