Welcome, guest | Sign In | My Account | Store | Cart
2

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.

Python, 77 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/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])

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!

1 comment

Gavin Debianfan 10 years, 3 months ago  # | flag

Import module from directory. 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 a comment

Sign in to comment

Created by Jesse Noller on Thu, 14 Jul 2005 (PSF)
Python recipes (4536)
Jesse Noller's recipes (1)

Required Modules

Other Information and Tasks