Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to import a name from a module-path?

Reply
Thread Tools

how to import a name from a module-path?

 
 
thebjorn
Guest
Posts: n/a
 
      06-16-2009
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
 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      06-16-2009
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
>


 
Reply With Quote
 
 
 
 
thebjorn
Guest
Posts: n/a
 
      06-16-2009
On Jun 16, 7:43*pm, Gary Herron <gher...@islandtraining.com> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:08 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:01 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments
 



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