Getting a list of all modules

Robert Kern robert.kern at gmail.com
Fri Aug 1 09:39:09 EDT 2014


On 2014-07-31 11:41, Steven D'Aprano wrote:
> On Wed, 30 Jul 2014 21:22:18 +0800, Leo Jay wrote:
>
>> On Wed, Jul 30, 2014 at 3:43 PM, Steven D'Aprano <steve at pearwood.info>
>> wrote:
>>> I'm looking for a programmatic way to get a list of all Python modules
>>> and packages. Not just those already imported, but all those which
>>> *could* be imported.
>>>
>>>
>> If you don't actually import it, how can you know it could be imported?
>> Not all .so files are valid python modules. Not all .py files could be
>> imported by all python interpreters.
>
> You're right, of course, but I'm not concerned by whether or not the
> module is error-free and can be imported successfully.
>
> I'm working on tab completion for module names. I have some alpha-quality
> code working, so if I hit TAB after typing "import ma" I get this:
>
>
> py> import ma
> macpath      macurl2path  mailbox      mailcap      mangle
> markupbase   math
>
> For what it's worth, importing "mangle" fails with a SyntaxError. But
> that's okay, I don't expect tab completion to only show *valid*
> modules :-)
>
> Over the next few days I'll make an official announcement, but if anyone
> wants a sneak-peek, check out:
>
> http://code.google.com/p/tabhistory/source/browse/tabhistory.py
>
>
> where I have indenting, code completion, filename completion, and module
> completion all working to some degree or another.

Take a look at what has already been implemented in IPython:

https://github.com/ipython/ipython/blob/master/IPython/core/completerlib.py#L208

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list