Get all class names in a Python package
Question: I need to get the list of all classes in Python package. At first I get all
filenames (it works fine, took it from stackoverflow):

    
    
    from os import listdir, getcwd
    from os.path import isfile, join
    mypath = os.getcwd()
    onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
    

Then I inspect all files and it doesn't work properly:

    
    
    for x in onlyfiles:
        for name, obj in inspect.getmembers(x):
            if inspect.isclass(obj):
                print obj
    

The output is:

    
    
    <type 'str'>
    ....
    <type 'str'>
    

However, the following code works properly:

    
    
        for name, obj in inspect.getmembers(example.py):
            if inspect.isclass(obj):
                print obj
    

Could you help me to figure out what the mistake is?


Answer: Try this, using the
[`inspect`](http://docs.python.org/3.2/library/inspect.html) module:

    
    
    import sys
    import inspect
    
    clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
    

This runs within a live file.

