禁用python的help()访问web

ash*_*ash 0 python

我正在使用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...
Run Code Online (Sandbox Code Playgroud)

然后我收到一系列警告

Warning: cannot register existing type 'GtkWidget'
...
Warning: cannot add class private field to invalid type '<invalid>'
...
Run Code Online (Sandbox Code Playgroud)

然后整个事情挂起......我必须开始第二个远程会话来发送SIGKILL.

显然有些事情是错的,但我最惊讶的是它在网络上收集信息的位置.

Python的帮助文档是否可以存储在本地?如何阻止它进入网络?我想要定期帮助,而不是在线帮助.

Bak*_*riu 5

help()命令不在互联网上搜索; "在线"只是意味着您可以以交互方式使用它,在它称之为"内置帮助系统"的文档中,它不那么模糊.它的作用是遍历所有PYTHONPATH并尝试导入每个模块,以便查看系统中可用的模块.

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

    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".
''')
Run Code Online (Sandbox Code Playgroud)

ModuleScanner类只是遍历内置模块和pkgutil.walk_packages找到的模块的地方,这个函数在末尾调用iter_modules导入器对象的方法.内置导入程序支持从Internet导入模块,因此不会搜索Internet.如果您安装自定义导入程序,help() 可能会触发互联网研究.

如果您有许多模块可用,则此操作可能需要一些时间.某些模块也可能花费大量时间来导入(例如numpy,scipy等等可能需要大约几秒钟来加载).