繁体   English   中英

禁止python的help()访问网络

[英]Disable python's help() from accessing web

我正在使用Python2.7。 当我输入help()并输入“ modules”时,我收到消息

>>> help()
Welcome to Python 2.7! This is the online help utility.
...
help> modules
Please wait a moment while I gather a list of all available modules...

然后我得到一系列警告

Warning: cannot register existing type 'GtkWidget'
...
Warning: cannot add class private field to invalid type '<invalid>'
...

然后整个事情挂起了……到我必须开始第二个远程会话以发送SIGKILL的地步。

显然有些问题是错的,但令我最惊讶的是它伸到网络上收集信息的位置。

Python的帮助文档不是本地存储的吗? 如何阻止它发布到网络上? 我需要常规帮助, 而不是在线帮助

help()命令不会在互联网上搜索; “在线”仅表示您可以交互使用它,在文档中称其为“内置帮助系统”,它不那么陌生。 它的作用是遍历所有PYTHONPATH并尝试导入每个模块,以查看系统中哪些模块可用。

这是用于获取模块列表的源代码(您可以在python源中的Lib/pydoc.py下找到):

    def listmodules(self, key=''):
        if key:
            self.output.write('''
Here is a list of matching modules.  Enter any module name to get more help.

''')
            apropos(key)
        else:
            self.output.write('''
Please wait a moment while I gather a list of all available modules...

''')
            modules = {}
            def callback(path, modname, desc, modules=modules):
                if modname and modname[-9:] == '.__init__':
                    modname = modname[:-9] + ' (package)'
                if modname.find('.') < 0:
                    modules[modname] = 1
            def onerror(modname):
                callback(None, modname, None)
            ModuleScanner().run(callback, onerror=onerror)
            self.list(modules.keys())
            self.output.write('''
Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose descriptions contain the word "spam".
''')

ModuleScanner类仅遍历内置模块以及pkgutil.walk_packages找到的模块的pkgutil.walk_packages ,此函数最终调用了导入器对象的iter_modules方法。 内置导入器支持从Internet导入模块,因此不会搜索Internet。 如果您安装自定义导入程序,则help() 可能会触发互联网研究。

如果有很多可用模块,则此操作可能需要一些时间。 一些模块可能还需要花费大量时间才能导入(例如, numpyscipy等加载可能要花费几秒钟的时间)。

在控制台上, export PYTHONDOCS=/usr/share/doc/python2/html/确定python应该在哪里搜索帮助。

暂无
暂无

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

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