Ciasto piekarz
Ciasto piekarz

Reputation: 8277

Listing installed modules in Python

Here is a basic code I came up with to list modules installed.

import sys as s
mod=s.modules.keys()
for indx,each in enumerate(mod):
    print indx,each

But what I am looking for is, it should only print out the parent module name like for

 numpy.f2py.pprint'
 numpy.distutils.atexit'
 numpy.distutils.sys'
 numpy.f2py.sys'

It should count it once as Numpy and move to look for next module and repeat same way...

Upvotes: 0

Views: 926

Answers (4)

user1176501
user1176501

Reputation:

def __listAllModules(self):
    """ This method returns all the modules installed in python
        including the built in ones.
    """
    allmodules = list(sys.builtin_module_names)
    allmodules += list(t[1] for t in pkgutil.iter_modules())
    allmodules = sorted(allmodules)
    return allmodules

Upvotes: 0

MostafaR
MostafaR

Reputation: 3695

import sys
print set([each.split('.')[0] for each in sys.modules.keys()])

Upvotes: 4

user2665694
user2665694

Reputation:

import sys as s
mod=s.modules.keys()
mods_seen = list()
for indx,each in enumerate(mod):
    parts = each.split('.')
    if not parts[0] in mods_seen:
        print indx,each
        mods_seen.append(parts[0])

Upvotes: 1

TimD
TimD

Reputation: 1381

The system command pip freeze does just about what you want, however I'm not entirely certain if it lists all modules or only those installed with pip.

Upvotes: 1

Related Questions