pydev - ide 抱怨模块导入自身
注意:我还没有接受答案,因为我真正的问题是为什么这被标记为编译错误。我投票赞成@TorelTwiddler 答案,因为他给出了解决方法,但我想了解这个问题。
我有一个包含自测试代码的简单模块。为了便于在交互式会话中使用,自测试代码会自行重新加载(模块)。
这在 PyDev (RunAs) 和在交互式窗口中运行都可以正常工作(手动修改 sys.path 并导入后);但 PyDev IDE 仍然给出“编译”/red-X 错误
import Mod1
我从自身导入 Mod1 的原因是该名称必须在运行 Test() 的任何上下文中都可解析,以便重新加载成功。例如,如果您从新的交互窗口导入 MyMain
,则在不导入的情况下对 reload
的调用将会失败,因为名称 Mod1 未知。
有什么办法可以解决这个问题吗? Mod1
是一个更大项目的一部分,将其始终标记为未编译会使开发变得更加困难...
存在问题的模块:
# Mod1.py
def Test():
"""
run the self-test, but first force a reload of the module under test (this mod)
"""
import Mod1 # *****'COMPILE' ERROR HERE******
import imp
Mod1 = imp.reload(Mod1)
TestImpl()
def TestImpl():
"""
self test here
since I reload above, I can change this code and re-run from an interactive window
this has to be a seperate function for the reload in Test to have an effect on this code
"""
print(input("enter"))
额外的引导模块仅在 PyDev 中使用(因此我可以“运行”) -as')
# MyMain.py
import Mod1
Mod1.Test()
PyDev/Project PYTHONPATH (显示正确,该文件夹位于我工作区的根目录)。既然它确实在 PyDev OK 中运行,那么它肯定是正确的吗?
/MyDirectory
谢谢!
NOTE: I haven't accepted an answer because my real question is why this is being marked as a compile error. I've voted up @TorelTwiddler answer because he gave a workaround, but I'd like to understand the problem.
I have a simple module that contains self-test code. To facilitate use in an interactive session, the self-test code reloads itself (the module).
This works OK in both PyDev (RunAs) and running in an interactive window (after manually amending sys.path
and importing); but the PyDev IDE still gives a 'compile' / red-X error on the line
import Mod1
The reason I import Mod1
from itself is that the name must be resolvable in whatever context is running Test()
, in order for the reload to succeed. For instance, if you import MyMain
from a fresh interactive window, the call to reload
would fail without the import, since the name Mod1 is not known.
Is there any way I can fix this?? Mod1
is one part of a much larger project and having it consistently marked as not-compiling makes development more difficult...
Module with the problem:
# Mod1.py
def Test():
"""
run the self-test, but first force a reload of the module under test (this mod)
"""
import Mod1 # *****'COMPILE' ERROR HERE******
import imp
Mod1 = imp.reload(Mod1)
TestImpl()
def TestImpl():
"""
self test here
since I reload above, I can change this code and re-run from an interactive window
this has to be a seperate function for the reload in Test to have an effect on this code
"""
print(input("enter"))
Extra bootstrap module only used in PyDev (so I can 'run-as')
# MyMain.py
import Mod1
Mod1.Test()
PyDev/Project PYTHONPATH (appears correct, this folder is at the root of my workspace). Since it does actually run in PyDev OK, it is definitely correct?
/MyDirectory
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在其内部重新加载模块,则会创建一个无限循环,因为您将重新加载该模块,然后再次重新加载该模块,等等......
在您的
MyMain.py
中,您可能可以这样做这个:我想如果没有进一步的代码示例,很难准确判断为什么必须重新加载模块,但如果必须这样做,那应该可以工作(无需在模块中执行)
If you reload a module within itself you create an infinite loop, because you would reload the module only to reload the module again, and again etc...
In your
MyMain.py
you might be able to do this:I guess without further code samples its hard to gauge exactly WHY you must reload the module at all, but if you have to, that should work (without doing it in the module)
我可能会同意 Snaxib 的答案,但是如果您希望保留当前格式,您可以通过在行末尾添加
#@UnresoledImport
(按 Ctrl-1自动填写)。I would probably go with Snaxib's answer, however if you'd prefer to keep the current format, you can have Eclipse ignore the error by adding
#@UnresoledImport
at the end of your line (hit Ctrl-1 to auto fill it in).从 Snaxib 答案的讨论中添加一个单独的答案,以进行格式化。
拥有一个不更改的模块
TestMyStuff
和另一个根据更改进行更新的模块TheTest
。现在
,从迭代 shell 中,您应该能够运行
TestMyStuff.go()
,每次运行时都会重新加载TheTest
。Adding a separate answer from the discussion on Snaxib's answer, for formatting.
Have one module that doesn't change,
TestMyStuff
and another that you update with your changes,TheTest
.and
Now, from the iteractive shell, you should be able to run
TestMyStuff.go()
, which will haveTheTest
reload every time it's run.我使用了 #@UnresolvedImport 注释来告诉 PySide 忽略它。就我而言,没有真正的解决方法,因为使用 pickle 时(我是通过 yaml 间接使用它),正确的导入路径取决于模块的加载方式。如果您想从模块外部取消对象的pickle,那么如果pickle是从内部完成的,则该对象将不起作用。自我导入为您提供了这种可能性。
I went with the #@UnresolvedImport annotation that tells PySide to ignore it. In my case there is no real workaround since when using pickle (im using it indirectly through yaml) the correct import path depends on how a module was loaded. If you want to un-pickle an object from outside a module it will not work if pickling was done from the inside. The self-import gives you that possibility.