Getting a list of all modules

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jul 31 12:41:30 CEST 2014


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.

Thanks to everyone for their help.



-- 
Steven



More information about the Python-list mailing list