Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > import class from string

Reply
Thread Tools

import class from string

 
 
Mariano DAngelo
Guest
Posts: n/a
 
      07-04-2012
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-04-2012
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
 
Reply With Quote
 
 
 
 
Thomas Jollans
Guest
Posts: n/a
 
      07-04-2012
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>
>>>


 
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
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments