Load objects using __import__

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MindClass

    #1

    Load objects using __import__

    I load objects from a file (to django framework _but it's a python
    question_).

    1) Before I used this
    from project.I18n.mo dels import *

    And to load the object
    c1 = Country(name="A fghanistan")

    But the problem is that there is to edit 'project' manually.

    2) So I setup the environment and import the module:
    project_directo ry = os.path.abspath ('')
    project_name = os.path.basenam e(project_direc tory)
    os.environ['DJANGO_SETTING S_MODULE'] = "%s.setting s" % project_name
    sys.path.append (os.path.dirnam e(project_direc tory))
    module = __import__('%s. I18n.models' % (project_name), {}, {},
    [''])
    sys.path.pop()

    It's all correct until here and to checking it I run:
    print i18n_module
    <module 'project.I18n.m odels' from
    '/home/lala/django/project/I18n/models.pyc'>

    print i18n_module.Cou ntry
    <class 'project.I18n.m odels.Country'>

    But when I load the object I get
    SyntaxError: invalid syntax

    c1 = <module 'project.I18n.m odels' from
    '/home/lala/django/project/I18n/models.pyc'>.Co untry(name="Afg hanistan")

    Any idea?

  • Carl Banks

    #2
    Re: Load objects using __import__


    MindClass wrote:
    But when I load the object I get
    SyntaxError: invalid syntax
    >
    c1 = <module 'project.I18n.m odels' from
    '/home/lala/django/project/I18n/models.pyc'>.Co untry(name="Afg hanistan")
    It looks like you're running generated code somewhere, but passing the
    module itself rather than the name of the module to the code generator
    by mistake. Perhaps you're execing some code that you pass the module
    name to? Something like this?

    exec 'c1 = %s.Country(name ="Afgahanstan") ' % module_name

    You seem to be erroneously using the module itself, not the module
    name.

    That's about all I can help without more information.


    P.S. if you are doing that, consider using getattr instead of exec.

    c1 = getattr(module, Country)(name=" Afgansitan")


    Carl Banks

    Comment

    • MindClass

      #3
      Re: Load objects using __import__

      Indeed before reading your message I solved it doing:

      model_name = Country
      mod = eval('i18n_modu le.%s' % model_name)
      c1 = mod(name="Afgha nistan")
      exec c1

      but I'll use getattr as you say. Thanks

      Carl Banks ha escrito:
      MindClass wrote:
      But when I load the object I get
      SyntaxError: invalid syntax

      c1 = <module 'project.I18n.m odels' from
      '/home/lala/django/project/I18n/models.pyc'>.Co untry(name="Afg hanistan")
      >
      It looks like you're running generated code somewhere, but passing the
      module itself rather than the name of the module to the code generator
      by mistake. Perhaps you're execing some code that you pass the module
      name to? Something like this?
      >
      exec 'c1 = %s.Country(name ="Afgahanstan") ' % module_name
      >
      You seem to be erroneously using the module itself, not the module
      name.
      >
      That's about all I can help without more information.
      >
      >
      P.S. if you are doing that, consider using getattr instead of exec.
      >
      c1 = getattr(module, Country)(name=" Afgansitan")
      >
      >
      Carl Banks

      Comment

      Working...