FAQ
Could anyone offer any advice on the following problem. What I have is a
python program that takes a file and does something to it by calling member
functions in a class. To seperate the code that does the export from the
main program, what I wanted to do is to be able to go from a text string
name of the export process....

str = 'export_1'

and do the equivalent of

import 'export_1'

exportclass = export1.Export.DoExport

So in case that's not clear I want to be able to import modules in a class
without knowing their name until runtime, then use the module name as a
variable.EG:

export_string = 'export_1'

import export_string # this doesn't work

export_string.Export # this doesn't work either

Search Discussions

  • Robin and jim at Jan 19, 2001 at 1:32 am
    hello Justin,

    Are you looking for something like the following?


    import os, string, sys

    directory_name = sys .argv [1]
    package_name = string .replace (directory_name, os.sep, '.')

    try:
    package = __import__ ( package_name, globals(), locals(), ['__init__'] )
    modules = getattr (package, '__all__')
    except AttributeError, message:
    handle_error ('"__init__.py" file for ' + directory_name + ' has no
    "__all__ attribute')
    except ImportError, message:
    handle_error (directory_name + ' has no "__init__.py" file')


    "JustinHJ" <justinhj at hotmail.com> wrote in message
    news:t6f17dps6c4d87 at corp.supernews.com...
    Could anyone offer any advice on the following problem. What I have is a
    python program that takes a file and does something to it by calling member
    functions in a class. To seperate the code that does the export from the
    main program, what I wanted to do is to be able to go from a text string
    name of the export process....

    str = 'export_1'

    and do the equivalent of

    import 'export_1'

    exportclass = export1.Export.DoExport

    So in case that's not clear I want to be able to import modules in a class
    without knowing their name until runtime, then use the module name as a
    variable.EG:

    export_string = 'export_1'

    import export_string # this doesn't work

    export_string.Export # this doesn't work either



  • Daniel Klein at Jan 19, 2001 at 2:34 pm

    On Thu, 18 Jan 2001 16:02:41 -0800, "JustinHJ" wrote:
    Could anyone offer any advice on the following problem. What I have is a
    python program that takes a file and does something to it by calling member
    functions in a class. To seperate the code that does the export from the
    main program, what I wanted to do is to be able to go from a text string
    name of the export process....
    I think what you are looking for is the __builtin__.__import__() function, but
    since I am relatively new with Python, I hope I didn't screw up and give you
    the wrong answer.

    Daniel Klein
  • JustinHJ at Jan 19, 2001 at 6:06 pm
    thanks for the help guys

    I think in a nutshell what I needed was the following. I've removed
    exception handling to simplify the example...

    fp, pathname, description = imp.find_module( modname )

    print 'importing module', modname
    LoadedModule = imp.load_module( modname, fp, pathname, description)
    LoadedModule.Export(i)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 19, '01 at 12:02a
activeJan 19, '01 at 6:06p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase