how to import a name from a module-path?

Discussion in 'Python' started by thebjorn, Jun 16, 2009.

  1. thebjorn

    thebjorn Guest

    I'm storing the path to functions in a database and now I'd like to
    get a reference so I can execute them.

    I looked briefly at the imp module and got very confused... Currently
    I'm doing this:

    def import_object(path):
    module, obj = path.rsplit('.', 1)
    exec "from rootpkg.%s import %s as fn" % (module, obj)
    return fn

    function = import_object('mypackage.mymodule.myfunction')

    this is happening in a trusted environment, so I'm not worried about
    malicious code.

    Are there more elegant ways of doing this (ie. without exec)?

    -- bjorn
     
    thebjorn, Jun 16, 2009
    #1
    1. Advertising

  2. thebjorn

    Gary Herron Guest

    thebjorn wrote:
    > I'm storing the path to functions in a database and now I'd like to
    > get a reference so I can execute them.
    >
    > I looked briefly at the imp module and got very confused... Currently
    > I'm doing this:
    >
    > def import_object(path):
    > module, obj = path.rsplit('.', 1)
    > exec "from rootpkg.%s import %s as fn" % (module, obj)
    > return fn
    >
    > function = import_object('mypackage.mymodule.myfunction')
    >
    > this is happening in a trusted environment, so I'm not worried about
    > malicious code.
    >
    > Are there more elegant ways of doing this (ie. without exec)?
    >


    Yes. Look at the __import__ builtin function, and if that's not enough
    functionality, look at the module names imp, which provides
    documentation starting with this bit:

    DESCRIPTION
    This module provides the components needed to build your own
    __import__ function.



    Both are available in Python2.x and Python3.x

    Gary Herron



    > -- bjorn
    >
     
    Gary Herron, Jun 16, 2009
    #2
    1. Advertising

  3. thebjorn

    thebjorn Guest

    On Jun 16, 7:43 pm, Gary Herron <> wrote:
    > thebjorn wrote:
    > > I'm storing the path to functions in a database and now I'd like to
    > > get a reference so I can execute them.

    >
    > > I looked briefly at the imp module and got very confused...  Currently
    > > I'm doing this:

    >
    > >   def import_object(path):
    > >       module, obj = path.rsplit('.', 1)
    > >       exec "from rootpkg.%s import %s as fn" % (module, obj)
    > >       return fn

    >
    > >   function = import_object('mypackage.mymodule.myfunction')

    >
    > > this is happening in a trusted environment, so I'm not worried about
    > > malicious code.

    >
    > > Are there more elegant ways of doing this (ie. without exec)?

    >
    > Yes.  Look at the __import__ builtin function,

    [...]

    Thanks, that is much better:

    def import_object(path):
    module, obj = path.rsplit('.', 1)
    m = __import__(module, fromlist=['rootpkg'])
    return getattr(m, obj)

    function = import_object('mypackage.mymodule.myfunction')


    > Gary Herron


    Bjorn
     
    thebjorn, Jun 16, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page