3

#python 的某个人建议它正在搜索模块“herpaderp”并找到所有列为其搜索的模块。如果是这种情况,为什么它在引发 ImportError 之前不列出我系统上的每个模块?有人可以阐明这里发生的事情吗?

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

输出:

16:00:55 $> python wtf.py
herpaderp None
apport None
subprocess None
traceback None
pickle None
struct None
re None
sre_compile None
sre_parse None
sre_constants None
org None
tempfile None
random None
__future__ None
urllib None
string None
socket None
_ssl None
urlparse None
collections None
keyword None
ssl None
textwrap None
base64 None
fnmatch None
glob None
atexit None
xml None
_xmlplus None
copy None
org None
pyexpat None
problem_report None
gzip None
email None
quopri None
uu None
unittest None
ConfigParser None
shutil None
apt None
apt_pkg None
gettext None
locale None
functools None
httplib None
mimetools None
rfc822 None
urllib2 None
hashlib None
_hashlib None
bisect None
Traceback (most recent call last):
  File "wtf.py", line 14, in <module>
    import herpaderp
ImportError: No module named herpaderp
4

3 回答 3

3

Looks like this is what's happening:

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

The solution? I have removed the “python-apport” package, along with the “ubuntuone-client” suite that depends on it. 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.

于 2010-05-26T22:22:08.370 回答
0

关于为什么会发生这种情况没有一个好的答案,但它在我测试它的两个操作系统之间有所不同。

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 在 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

对不起,这不是一个真正的答案,但有点太长了,无法发表评论!

于 2010-05-26T21:51:30.720 回答
0

这是 pupy python 模块恕我直言的一部分,它用于进程注入。如果没有适当的 rpyc 会话,它将无法工作,该会话可以将必要的模块作为打包消息提供,并动态解包和加载。

于 2022-02-03T19:51:29.950 回答