Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Dallas, TX
    Posts
    65
    Rep Power
    10

    Import value of string


    If I have a module name in a string, is there a way to import it?
    If I have something like:
    myModule = "my.module"

    How do I get python to import the value of myModule instead of trying to import "myModule"?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I'm a little confused on what you're asking. Can you explain this more clearly?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Dallas, TX
    Posts
    65
    Rep Power
    10
    If I have the name of a module in a variable, how do I do an import of that module by using the veriable. Like the following.

    module_to_import = "xml.sax.saxutils"
    import module_to_import

    This as you know does not work. How can I do this?
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I don't understand why you would want to do that. Python isn't going to recognize that import as a module, especially if it is in double quotes ("xml.sax.saxutils"). Just do
    import xml.sax.saxutils. I don't see why you would want to do it any other way.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Dallas, TX
    Posts
    65
    Rep Power
    10
    That was just an example. I want to do it on an a derived module. Something like the following.

    myModule = getModuleName()

    try: import myModule
    except ImportError: print "Module " + myModule + "does not exist."
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Can you provide all of your code so I can get a better idea of this? Right now getModuleName() doesn't tell me anything. I still don't see the purpose of doing what you're doing, simply doing import module_name is good enough.
    Code:
    try:
        import module_name
    except Exception, inst:
        print inst
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Dallas, TX
    Posts
    65
    Rep Power
    10
    Well I am playing around with mod_python. If you are familiar with it, there is a publisher module that maps urls to python objects. I am trying to do a similar thing.

    Code:
    import string
    from mod_python import apache
    
    # This results with requested as "pages/index" when you go to
    # http://localhost/pages/index
    requested = req.uri
    
    # req is the apache request object.
    
    #translate uri into module name
    string.replace(requested, "/", ".")
    
    try:
         import requested
         pageClass = requested(req)
         pageClass.display()
    except ImportError: return apache.HTTP_NOT_FOUND
    Then there is a pages.py file with the index class in it.

    Code:
    class index:
         def __init__(self, req):
              self.req = req
         def display(self):
              req.content_type = "text/html"
              req.send_http_header( )
              req.write( "<html><head><title>Test</title></head><body>" )
              req.write( "Index" )
              req.write( "</body></html>" )
              req.flush( )
              return
         def rss(self):
              #this will be an rss backend
              return
  14. #8
  15. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Hi dreamcatalyst,

    I think what you are looking for is the __import__
    built in function.

    mymodule = __import__("mymodule file")

    grim
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by dreamcatalyst
    If I have a module name in a string, is there a way to import it?
    If I have something like:
    myModule = "my.module"

    How do I get python to import the value of myModule instead of trying to import "myModule"?
    Code:
    sys = __import__("sys")
    sys.stdout.write("Imported\n")
    Note how you have to assign it a name in order to access it...

    --OH.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Dallas, TX
    Posts
    65
    Rep Power
    10
    Originally Posted by hydroxide
    Code:
    sys = __import__("sys")
    sys.stdout.write("Imported\n")
    Note how you have to assign it a name in order to access it...

    --OH.

    the problem with this is that I want to call the imported module using the same variable like so:
    Code:
    myModule = "my.module.class"
    module = __import__(myModule)
    myModule(req)
    This does not work nor does "module.myModule(req)"
    Any other suggestions?
  20. #11
  21. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    >>> myModule = "my.module.class"
    module = __import__(myModule)
    myModule(req)
    >>>
    I received no error. If you're receiving an error. Post the error and tell us when the error occurs.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Dallas, TX
    Posts
    65
    Rep Power
    10
    Originally Posted by †Yegg†
    Code:
    >>> myModule = "my.module.class"
    module = __import__(myModule)
    myModule(req)
    >>>
    I received no error. If you're receiving an error. Post the error and tell us when the error occurs.
    I get
    Code:
     Module = myModule(req)
    
    TypeError: 'str' object is not callable
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by dreamcatalyst
    the problem with this is that I want to call the imported module using the same variable like so:
    Code:
    myModule = "my.module.class"
    module = __import__(myModule)
    myModule(req)
    This does not work nor does "module.myModule(req)"
    You don't import classes, you import modules...
    Code:
    mymodule = "mypackage.mymodule"
    MyClass =  __import__(mymodule).MyClass
    MyClass(req)
    [note that the only thing you'll be able to reference is MyClass since mypackage.mymodule was never bound to a name]

    --OH.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Dallas, TX
    Posts
    65
    Rep Power
    10
    Thanks for the help but I am starting to think its something with my python.

    if I do:
    Code:
    # This is equivilent to __import__
    module = apache.import_module(page, autoreload= 1 )
    
    #This gets the class name
    last = page.split(".")
    last = last[len(last)-1]
    
    #This puts what I want to do in a string
    ModuleName = "moduleClass = module." + str(last)+"(req)"
    
    #Execute the string as code
    exec(ModuleName)
    
    moduleClass = ModuleName(req)
    moduleClass.display()
    This works but if I try to do it as you suggest, it doesn't work. I am sure your method works but not on my server.
  28. #15
  29. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I receive different errors because I do not have a module with that name. I dont get a string error however.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo