eclipse pydev 中未解决的导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14201504/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 21:29:59  来源:igfitidea点击:

unresolved import in eclipse pydev

eclipseimport

提问by toom

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.

我刚刚为python正确安装了google gdata库(脚本实际上运行良好)。我正在 Eclipse 中使用 pydev。我导入了一个模块,但导入命令仍然带有红色下划线的卷曲,如屏幕截图所示。

enter image description here

在此处输入图片说明

I added the following und ProjectName->Properties, but it does not work (although the path is definitly correct): enter image description here

我添加了以下和 ProjectName->Properties,但它不起作用(尽管路径绝对正确): 在此处输入图片说明

What do I have to do such that the import is resovled correctly by eclipse?

我必须做什么才能使 Eclipse 正确解析导入?

回答by Piotr Sobczyk

If you have import statement like this: import gdata.spreadsheet.servicethen you need to make sure that on PYTHONPATHthere is a directory that contains gdatasubdirectory (and gdatashould have spreadsheetsubdirectory with service.pymodule file).

如果你有这样的导入语句:import gdata.spreadsheet.service那么你需要确保PYTHONPATH有一个包含gdata子目录的目录(并且gdata应该有spreadsheet包含service.py模块文件的子目录)。

In your case: if your gdatadirectory is in site-packagesdirectory on python distribution, then you need to make sure that site-packagesis on PYTHONPATH (not site-packages/gdata/spreadsheetthat you were trying to put there).

在你的情况下:如果你的gdata目录site-packages在 python 发行版的目录中,那么你需要确保它site-packages在 PYTHONPATH 上(不是site-packages/gdata/spreadsheet你试图放在那里)。

Look at http://docs.python.org/2/tutorial/modules.htmlin "6.4 Packages" section for example and reference.

查看“6.4 包”部分中的http://docs.python.org/2/tutorial/modules.html示例和参考。

In PyDev you can modify the contents of PYTHONPATH in two places:

在 PyDev 中,您可以在两个地方修改 PYTHONPATH 的内容:

  • Window -> PyDev -> Interpreter - Python
  • In the window you used
  • 窗口 -> PyDev -> 解释器 - Python
  • 在您使用的窗口中

I would suggest you to first check that site-packagesdirectory 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!).

我建议您首先检查该site-packages目录是否在您的Window -> PyDev -> Interpreter - Python settings -> System PYTHONPATH. 默认情况下它应该在那里,所以如果你没有改变它,它应该在那里(可能这就是你的应用程序工作的原因!)。

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).

现在,由于某些原因,PyDev 在向其中添加新库后经常在“刷新”有关可用库的信息时遇到问题site-packages(例如,它使用该信息来检查是否应该在 import 语句上放置错误标记)。

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 -> Removeand then adding it back in the same view. Then the site-packagesdirectory is rescanned and PyDev sees the updated set of libraries, so the error markers should disappear.

有几种方法可以强制刷新此信息。对我来说最可靠的方法是通过以下方式删除 python 解释器:Window -> PyDev -> Interpreter - Python -> Remove然后将其添加回同一视图中。然后site-packages重新扫描目录,PyDev 会看到更新的库集,因此错误标记应该消失。

Side note: To be honest, so far I haven't ever need to use this External Librariesview. Having site-packages in Interpreter settings is fine enought for 99% of cases because there is where Python external libraries should be put.

旁注:老实说,到目前为止我还不需要使用这个External Libraries视图。在解释器设置中使用站点包对于 99% 的情况来说已经足够了,因为应该放置 Python 外部库。