python / cpython

The Python programming language
https://www.python.org
Other
62.9k stars 30.13k forks source link

Command to display all available Import Library #64705

Open 63547aaf-0f2e-444b-bbff-e20d314818d5 opened 10 years ago

63547aaf-0f2e-444b-bbff-e20d314818d5 commented 10 years ago
BPO 20506
Nosy @rhettinger, @pitrou, @benjaminp, @vadmium

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-feature', 'library'] title = 'Command to display all available Import Library' updated_at = user = 'https://bugs.python.org/arupchakrav' ``` bugs.python.org fields: ```python activity = actor = 'brett.cannon' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'arupchakrav' dependencies = [] files = [] hgrepos = [] issue_num = 20506 keywords = [] message_count = 8.0 messages = ['210171', '210172', '210173', '210174', '210178', '210238', '226682', '364554'] nosy_count = 5.0 nosy_names = ['rhettinger', 'pitrou', 'benjamin.peterson', 'martin.panter', 'arupchakrav'] pr_nums = [] priority = 'low' resolution = None stage = None status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue20506' versions = ['Python 3.5'] ```

63547aaf-0f2e-444b-bbff-e20d314818d5 commented 10 years ago

Have a built-in mechanism to display all available import libraries within the interpreter. Like what is available through the built-in dir function.

benjaminp commented 10 years ago
>>> import sys
>>> sys.modules.keys()
['google', 'copy_reg', 'sre_compile', '_sre', 'encodings', 'site', '__builtin__', 'sysconfig', '__main__', 'encodings.encodings', 'abc', 'posixpath', '_weakrefset', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'types', '_codecs', 'encodings.__builtin__', '_warnings', 'genericpath', 'stat', 'zipimport', '_sysconfigdata', 'warnings', 'UserDict', 'encodings.utf_8', 'sys', 'codecs', 'readline', 'os.path', 'signal', 'traceback', 'linecache', 'posix', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
pitrou commented 10 years ago

This is not what the OP asked, AFAICT. sys.modules is just the set of currently imported modules, not all potentially importable modules.

benjaminp commented 10 years ago

I guess I have no idea what the OP is asking for then. dir() certainly only gives immediately available builtins.

rhettinger commented 10 years ago

+1 for the OP's original idea (something like a dir_modules() function that lists out all possible imports).

I think something like this must already be present in some of the Python front-ends such as ipython and bpython. That is how they support tab-completion for imports:

In [1]: import co code codeop colorsys compileall contextlib copy codecs collections commands compiler cookielib copy_reg

In [1]: import re re readline repr requests resource reverend rexec

brettcannon commented 10 years ago

The trick would be how to query finders to say "what could you find?" There is no API for that so either something pragmatic that won't work in the face of e.g. zipfiles would need to be used or a new optional API on finders to list what the tail name of modules it can find are (but without the package name as most finders just use fullname.rpartition('.')[-1] to figure out what they are looking for).

vadmium commented 10 years ago

I wrote some code that does something like this for a hacky custom readline completer. See the import_list() method at \https://github.com/vadmium/etc/blob/6ac333f/python/pythonstartup.py#L222\. It looks like I’m using a combination of “sys.builtin_module_names” and pkgutil.iter_modules(), with a whole lot of compatibility and bug workarounds.

brettcannon commented 4 years ago

When a replacement for pkgutil.walk_packages() is added to importlib to work appropriately with the import system then this should be doable. See https://bugs.python.org/issue19939 for work to replace pkgutil as appropriate.