包 __init__ 中 PyDev 中未解决的导入
这确实是一个微不足道的问题,但却让我发疯。
我使用 src 文件夹选项创建了一个本质上默认的 PyDev 项目(PyDev 2.2.2、Eclipse 3.7.0),所以现在我有这样的东西:
mylib [project]
src [folder]
mylib [package]
__init__.py
mymod.py
所以现在我想在 __init__ 中导入 mymod,所以我的 __init__.py 看起来像这样:
import mylib.mymod
问题是,这会在 __init__.py 的 PyDev 编辑器窗口中生成一条“未解析的导入”消息,但它在控制台中有效,并且当我运行 代码。
我唯一的猜测是代码分析器正在使用 src/mylib (而不仅仅是 src)的工作目录来解析导入,这根本没有帮助,因为使用这个库的任何人都不会这样做。
我认为我在这里所做的事情很常见,但 PyDev 不喜欢它。有没有办法更改代码分析器使用的工作目录?或者我做了其他不标准的事情?
This is really a trivial problem, but it's driving me crazy.
I created an essentially default PyDev project (PyDev 2.2.2, Eclipse 3.7.0) using the src folder option, so now I have something like this:
mylib [project]
src [folder]
mylib [package]
__init__.py
mymod.py
So now I want to import mymod in __init__, so my __init__.py looks like this:
import mylib.mymod
The problem is that this generates an "Unresolved import" message in the PyDev editor window for __init__.py, yet it works in the console, and it works when I run the code.
My only guess here is that the code analyzer is parsing the import using a working directory of src/mylib (instead of just src), which isn't helpful at all, since anyone who uses this library won't be doing this.
I think what I'm doing here is pretty common, but PyDev isn't liking it. Is there a way to change the working directory the code analyzer uses? Or am I doing something else that's non-standard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须看一下 PyDev 源代码,但它的工作确实看起来很奇怪
在 Python 中(即:在包 mylib 中,您正在导入 mylib 包
再说一次,所以,在我看来,这将是一个循环导入——我相信正确的
这样做的方法应该是使用相对导入...即:“from . import mymod”
在这种情况下)。
无论如何,如果它在 Python 中工作,PyDev 也应该识别它,所以,我会采取
看看它:)
I have to take a look at the PyDev source, but it does seem weird that it works
in Python (i.e.: inside the package mylib you're importing the mylib package
again, so, it seems to me it'd be a cyclic import -- I believe that the proper
way of doing it should be using a relative import... i.e.: "from . import mymod"
in this case).
Anyways, if it works in Python, PyDev should recognize it too, so, I'll take
a look at it :)