FAQ
Hi list and python gurus :-)

I'm playing with some mod_python and web development. And in me code I
need to do som dynamic imports.
Right now I just do a:

exec 'import '+some_modulename

But it seems to easy, is there a "dark side" to doing it this way?
(memory use,processing ,etc)
And have to I check if the modul is already loaded?


Another thing is how to call my dynamic imported moduls.
Now I use exec (as with my modules), like this:

exec 'newclass = '+classname+'()'
newclass.somefunction()

Again it seems to easy. Is there a better/proper way to do it?


Do anybody now a good howto or tutorial to this?


Many thanks and hope you all have a happy new year :-)

/marc

Search Discussions

  • Benjamin at Dec 30, 2007 at 2:45 pm

    On Dec 30, 8:24 am, marcroy.ol... at gmail.com wrote:
    Hi list and python gurus :-)

    I'm playing with some mod_python and web development. And in me code I
    need to do som dynamic imports.
    Right now I just do a:

    exec 'import '+some_modulename
    The correct way to do this is use the __import__ function. It takes
    the string name of the module you want to import and returns the
    module.
    new_mod = __import__(some_modulename)
    But it seems to easy, is there a "dark side" to doing it this way?
    (memory use,processing ,etc)
    Well, it's generally frowned on to use exec and eval.
    And have to I check if the modul is already loaded?
    sys.modules is a list of all imported modules, but python won't import
    a module if it's already been loaded.
    Another thing is how to call my dynamic imported moduls.
    Now I use exec (as with my modules), like this:

    exec 'newclass = '+classname+'()'
    newclass.somefunction()

    Again it seems to easy. Is there a better/proper way to do it?
    If you just have the string name of a class, you have to use eval or
    exec:
    newclass = eval(classname)

    However, if you have the class object, you can just instantiate that:
    class LargeClass:
    def meth(): pass
    some_class = LargeClass
    new_class = some_class()
    some_class.meth()
    Do anybody now a good howto or tutorial to this?

    Many thanks and hope you all have a happy new year :-) You, too!
    /marc
  • Torsten Bronger at Dec 30, 2007 at 2:48 pm
    Hall?chen!

    marcroy.olsen at gmail.com writes:
    I'm playing with some mod_python and web development. And in me
    code I need to do som dynamic imports. Right now I just do a:

    exec 'import '+some_modulename

    But it seems to easy, is there a "dark side" to doing it this way?
    (memory use,processing ,etc) And have to I check if the modul is
    already loaded?
    I use the imp module for this:

    try:
    file, pathname, description = imp.find_module(full_name)
    my_module = imp.load_module(full_name, file, pathname, description)
    finally:
    file.close()

    Tsch?,
    Torsten.

    --
    Torsten Bronger, aquisgrana, europa vetus
    Jabber ID: bronger at jabber.org
    (See http://ime.webhop.org for further contact info.)
  • Gabriel Genellina at Dec 30, 2007 at 2:57 pm

    En Sun, 30 Dec 2007 12:24:53 -0200, <marcroy.olsen at gmail.com> escribi?:

    I'm playing with some mod_python and web development. And in me code I
    need to do som dynamic imports.
    Right now I just do a:

    exec 'import '+some_modulename

    But it seems to easy, is there a "dark side" to doing it this way?
    (memory use,processing ,etc)
    Use __import__, specially if some_modulename comes from the outside.
    What if some_modulename contains "modulename\nsome_nasty_function_call()"
    And have to I check if the modul is already loaded?
    Not needed; the standard import machinery already does that.
    Another thing is how to call my dynamic imported moduls.
    Now I use exec (as with my modules), like this:

    exec 'newclass = '+classname+'()'
    newclass.somefunction()

    Again it seems to easy. Is there a better/proper way to do it?
    Use getattr to obtain the desired class from the containing module, then
    use it as any other class:
    the_module = __import__(some_modulename)
    the_class = getattr(the_module, classname)
    o = the_class()
    o.somefunction()

    Never use exec/eval and friends - and never ever use them in a web
    application!
    Do anybody now a good howto or tutorial to this?
    No... what do you want covered?
    Many thanks and hope you all have a happy new year :-)
    Thanks, and a happy new year for you too!

    --
    Gabriel Genellina
  • Marcroy Olsen at Dec 30, 2007 at 3:34 pm
    First of thanks to all for you, especially for the quick replys.

    Just need to walk the dog then I giv it a short.

    On Dec 30, 3:57?pm, "Gabriel Genellina" wrote:

    Do anybody now a good howto or tutorial to this?
    No... what do you want covered?
    Nothing, think you reply coved it all.

    Otherwise I will be back ;-)
  • Diez B. Roggisch at Dec 30, 2007 at 10:32 pm

    marcroy.olsen at gmail.com schrieb:
    First of thanks to all for you, especially for the quick replys.

    Just need to walk the dog then I giv it a short.
    Please, don't kill your dog! We're a peace-loving community here that
    respects dogs, and snakes and even trolls.

    SCNR,

    Diez
  • Graham Dumpleton at Dec 31, 2007 at 12:45 am

    On Dec 31, 1:24?am, marcroy.ol... at gmail.com wrote:
    Hi list and python gurus :-)

    I'm playing with somemod_pythonand web development. And in me code I
    need to do som dynamic imports.
    Right now I just do a:

    exec 'import '+some_modulename

    But it seems to easy, is there a "dark side" to doing it this way?
    (memory use,processing ,etc)
    And have to I check if the modul is already loaded?

    Another thing is how to call my dynamic imported moduls.
    Now I use exec (as with my modules), like this:

    exec 'newclass = '+classname+'()'
    newclass.somefunction()

    Again it seems to easy. Is there a better/proper way to do it?

    Do anybody now a good howto or tutorial to this?

    Many thanks and hope you all have a happy new year :-)

    /marc
    If using mod_python, you should use mod_python's own dynamic module
    importer. See the documentation for mod_python.apache.import_module()
    in:

    http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html

    There is no need to use __import__ directly. By using mod_python's way
    of doing things you can benefit from automatic module reloading
    provided certain constraints met.

    Graham

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 30, '07 at 2:24p
activeDec 31, '07 at 12:45a
posts7
users6
websitepython.org

People

Translate

site design / logo © 2023 Grokbase