简体   繁体   English

为什么这是这个 python 程序的输出?

[英]Why is this the output of this python program?

Someone from #python suggested that it's searching for module "herpaderp" and finding all the ones listed as its searching. #python 的某个人建议它正在搜索模块“herpaderp”并找到所有列为其搜索的模块。 If this is the case, why doesn't it list every module on my system before raising ImportError?如果是这种情况,为什么它在引发 ImportError 之前不列出我系统上的每个模块? Can someone shed some light on what's happening here?有人可以阐明这里发生的事情吗?

import sys

class TempLoader(object):     
    def __init__(self, path_entry):
        if path_entry == 'test': return
        raise ImportError

    def find_module(self, fullname, path=None):
        print fullname, path
        return None

sys.path.insert(0, 'test')
sys.path_hooks.append(TempLoader)
import herpaderp

Looks like this is what's happening: 看起来这就是发生的事情:

http://rhodesmill.org/brandon/2010/ubuntu-exception-190-modules/ http://rhodesmill.org/brandon/2010/ubuntu-exception-190-modules/

Basically, the apport module (not part of the standard lib) gets tied in at a really low level to exceptions, like, before the exception is written to the stdout. 基本上,apport模块(不是标准lib的一部分)在异常被写入stdout之前,与异常相关联。 So when the program fails to find "herpaderp", it throws an exception and triggers the import of apport and all the modules it contains and displays them in the output before the exception. 因此,当程序无法找到“herpaderp”时,它会抛出异常并触发导入apport及其包含的所有模块,并在异常之前将其显示在输出中。

The solution? 解决方案? I have removed the “python-apport” package, along with the “ubuntuone-client” suite that depends on it. 我删除了“python-apport”包,以及依赖它的“ubuntuone-client”套件。 After the uninstall, exceptions are — wonderfully enough — not causing a single import of a new module! 在卸载之后,例外 - 非常好 - 不会导致单个导入新模块! Now, finally, I can continue writing my import hook in peace. 现在,最后,我可以继续安静地编写我的导入钩子。

Don't have a good answer as to why this is happening, but it differs between the two OSs I tested it on. 对于为什么会发生这种情况,没有一个好的答案,但是我测试它的两个操作系统之间有所不同。

2.5.1 & 2.6.4 on Windows: Windows上的2.5.1和2.6.4:

E:\work\python>python wtf.py
herpaderp None
Traceback (most recent call last):
  File "wtf.py", line 14, in <module>
    import herpaderp
ImportError: No module named herpaderp

E:\work\python>python --version
Python 2.5.1

2.5.2 on Linux: 2.5.2在Linux上:

$ python wtf.py
herpaderp None
apport None
subprocess None
... etc etc
_locale None
operator None
shutil None
Traceback (most recent call last):
  File "wtf.py", line 14, in <module>
    import herpaderp
ImportError: No module named herpaderp
$ python --version
Python 2.5.2

Sorry, that's not really an answer, but a bit too long to go in a comment! 对不起,这不是一个真正的答案,但有点太长了不能发表评论!

😏 that's part of a pupy python module imho, it's for process injection. 😏 这是 pupy python 模块 imho 的一部分,它用于进程注入。 It won't work without a proper rpyc session which can supply the necessary modules as packaged messages which it unpacks and loads dynamically.如果没有适当的 rpyc 会话,它将无法工作,该会话可以将必要的模块作为打包消息提供,并动态解包和加载。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM