ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Import Modules/Discover methods from a directory name
Submitter: Jesse Noller (other recipes)
Last Updated: 2005/07/15
Version no: 1.1
Category: Extending

 

4 stars 2 vote(s)


Description:

For something I am working on, I needed the ability to scan a supplied directory, adding the directory to the sys.path within Python, and then blanket import the modules within that directory. Following that, I had to filter any builtin or special methods within those modules and return a list of the methods for the module I had imported.

The script is very simplistic in what it does.

Source: Text Source

#!/usr/bin/env python

"""
loader.py - From a directory name:

1: append the directory to the sys.path
2: find all modules within that directory
3: import all modules within that directory
4: filter out built in methods from those modules
5: return a list of useable methods from those modules

Allows the user to import a series of python modules without "knowing" anything
about those modules.

Copyright 2005 Jesse Noller <jnoller@gmail.com>

"""

import os, sys

def import_libs(dir):
    """ Imports the libs, returns a list of the libraries. 
    Pass in dir to scan """
    
    library_list = [] 
    
    for f in os.listdir(os.path.abspath(dir)):       
        module_name, ext = os.path.splitext(f) # Handles no-extension files, etc.
        if ext == 'py': # Important, ignore .pyc/other files.
            print 'imported module: %s' % (module_name)
            module = __import__(module_name)
            library_list.append(module)
 
    return library_list

###############################################################################

def filter_builtins(module):
    """ Filter out the builtin functions, methods from module """

    # Default builtin list    
    built_in_list = ['__builtins__', '__doc__', '__file__', '__name__']
    
    # Append anything we "know" is "special"
    # Allows your libraries to have methods you will not try to exec.
    built_in_list.append('special_remove')

    # get the list of methods/functions from the module
    module_methods = dir(module) # Dir allows us to get back ALL methods on the module.

    for b in built_in_list:
        if b in module_methods:
            module_methods.remove(b)

    print module_methods
    return module_methods

###############################################################################

def main(dir):

    if os.path.isdir(dir):
        sys.path.append(dir)
    else:
        print '%s is not a directory!' % (dir)
    
    lib_list = import_libs(dir)
    
    for l in lib_list:
        filter_builtins(l)
        
if __name__ == "__main__":
    if len(sys.argv) < 2:
        print "error: missing directory name"
        sys.exit(1)
    else:
        main(sys.argv[1])

Discussion:

This may not be terribly useful to many people - in most cases, not only will you know the module name, but you will also know the methods accessible to you within the module.

Usage example:
jesse$python loader.py mydir/

imported module: basic
imported module: mod1
imported module: mod2
['a_a', 'a_b', 'a_c', 'a_d', 'a_e']
[]
[]

You can use this script, as well as getattr() to actually call the functions returned in the list.

Suggestions welcome!



Add comment

Number of comments: 1

Import module from directory, Gavin Debianfan, 2006/09/28
I believe the proper way is to add a site path using site.py and friends but just to add a module from a subdir within a portable project this works a charm, thanks to above.

#replace --indent-- with spaces or tabs

import os
import sys
dir='./uspp' #your dir here
if os.path.isdir(dir):
--indent--sys.path.append(dir)
import uspp

Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.