import class from string

Discussion in 'Python' started by Mariano DAngelo, Jul 4, 2012.

  1. Hi I'm trying to create a class from a string....
    This is my code, but is not working....

    'myshop.models.base'
    module_name, class_name = model.rsplit(".", 1)
    module = importlib.import_module(module_name)
    class_ = getattr(module, class_name)()


    Anyone know what I'm doing wrong.... or another way?

    thanks
     
    Mariano DAngelo, Jul 4, 2012
    #1
    1. Advertising

  2. On Wed, 04 Jul 2012 13:27:29 -0700, Mariano DAngelo wrote:

    > Hi I'm trying to create a class from a string.... This is my code, but
    > is not working....
    >
    > 'myshop.models.base'
    > module_name, class_name = model.rsplit(".", 1)
    > module = importlib.import_module(module_name)
    > class_ = getattr(module, class_name)()
    >
    >
    > Anyone know what I'm doing wrong....


    What version of Python are you using?

    What result do you expect?

    What result do you actually get?

    When asking for help, please take the time to create a short, self-
    contained, correct example that anyone can actually run:

    http://sscce.org/

    In the above, you have a bare string that does nothing; a name "model"
    that is undefined; and what looks like a module that isn't imported
    (importlib). We have no idea of what problem *you* see, because we can't
    run your code and you don't show us the error.

    But the general idea is correct, at least in Python 3.2:

    import importlib
    model = 'unittest.suite.BaseTestSuite'
    module_name, class_name = model.rsplit(".", 1)
    module = importlib.import_module(module_name)
    class_ = getattr(module, class_name)

    At the end of which, class_ is the expected BaseTestSuite class.


    I suspect that your error is that after you get the class object using
    getattr, you then *call* the class object but give no arguments, and the
    class requires arguments.

    You say:
    class_ = getattr(module, class_name)() # note the extra () brackets

    I say:
    class_ = getattr(module, class_name)


    Either that or you have a bug in your module and it can't be imported. Or
    you have misspelled the module name, or the class. Or forgotten to import
    importlib. Or are shadowing it with your own module. Who knows? Without
    seeing the error, I'm just guessing.



    --
    Steven
     
    Steven D'Aprano, Jul 4, 2012
    #2
    1. Advertising

  3. On 07/04/2012 10:27 PM, Mariano DAngelo wrote:
    > Hi I'm trying to create a class from a string....
    > This is my code, but is not working....


    It would be helpful if you posted an error message. Then, we could know
    what's actually going on.


    > 'myshop.models.base'
    > module_name, class_name = model.rsplit(".", 1)
    > module = importlib.import_module(module_name)
    > class_ = getattr(module, class_name)()


    .......................................^^

    These brackets may be causing problems?

    In principle, you're doing the right thing. This works for me:

    Python 3.2.3 (default, May 3 2012, 15:51:42)
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import importlib
    >>> objname = 'os.path.join'
    >>> module_name, member_name = objname.rsplit('.', 1)
    >>> module = importlib.import_module(module_name)
    >>> member = getattr(module, member_name)
    >>> member

    <function join at 0x7f882f4b0af0>
    >>>
     
    Thomas Jollans, Jul 4, 2012
    #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.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    974
    Paul Opal
    Oct 11, 2004
  2. E11
    Replies:
    1
    Views:
    4,844
    Thomas Weidenfeller
    Oct 12, 2005
  3. JPractitioner
    Replies:
    13
    Views:
    20,194
    Roedy Green
    Feb 24, 2006
  4. Vitali Gontsharuk
    Replies:
    2
    Views:
    579
    Vitali Gontsharuk
    Aug 25, 2005
  5. Artur M. Piwko

    import/from import question

    Artur M. Piwko, Jun 29, 2003, in forum: Python
    Replies:
    1
    Views:
    862
    Peter Hansen
    Jul 2, 2003
Loading...

Share This Page