0

I have a series of Class names and want to check them all and them import them. how to do this:

CName='Class_blahblah'
from eval(CName) import *  

this is what I got:

 SyntaxError: invalid syntax

To be exact: I have couple of classes I run in IDE in a FEA software and I do for each of them something like this:

if 'Class_SetSurfPart' in sys.modules:  
    del sys.modules['Class_SetSurfPart']
    print 'old module Class_SetSurfPart deleted'
    from Class_SetSurfPart import *
    reload(sys.modules['Class_SetSurfPart'])
else:
    from Class_SetSurfPart import *

but I want to put all class names in a List and do this as loop instead of doing this for all Classes.

Matt S
  • 35
  • 4
  • Is this [answer](http://stackoverflow.com/a/8719100/2599266) what you're looking for? – Obsidian Feb 10 '16 at 15:48
  • No, lets say `i=__import__('dupList', fromlist=[''])` and I get `>>> i` so I get ``. and error I get is `>>> dupList([1, 2, 3, 4, 1, 2, 3])` and `NameError: name 'dupList' is not defined` – Matt S Feb 10 '16 at 16:01

2 Answers2

0

It's probably a good idea to put the keys for

sys.modules
in a list like so:
keys = [key for key in sys.modules]

Remember that each item is stored as a string type.

#I'm assuming that you've already made a list of the classes you want to be 
#checking for in a list. I'll call that list classes.
for name in classes:
    if name in keys:
        del sys.modules[name]  #remember that this does not delete it permanently.
        print "Old module {} deleted".format(name)
        name = __import__(name)
        reload(name)
    else:
        __import__(name)
JustDucky
  • 132
  • 1
  • 10
  • this did not work. it says imported by does not work. – Matt S Feb 10 '16 at 16:18
  • What's the name of the module you are trying to import? Also, make sure there's no file extension(.py, .txt, etc.) on the file. – JustDucky Feb 10 '16 at 16:34
  • there are around Classes that I want to load as a loop. One of them is `Class_VerticesEdgesPart`. – Matt S Feb 10 '16 at 16:38
  • `if 'Class_SetSurfPart' in sys.modules: del sys.modules['Class_SetSurfPart'] print 'old module Class_SetSurfPart deleted' from Class_SetSurfPart import * reload(sys.modules['Class_SetSurfPart']) else: from Class_SetSurfPart import *` – Matt S Feb 10 '16 at 16:39
  • What is the point of the reload function? Simply using del on sys.modules won't change it permanently. – JustDucky Feb 10 '16 at 20:49
  • I edited the question at the top so you can see what I am trying to do. I have number of classes that I have to check if they are already in memory or not and then load them. I want to reduce the number of lines I am using by using a sort of loop. – Matt S Feb 10 '16 at 20:51
0

You want importlib.import_module:

bruno@bigb:~/Work/playground/imps$ ls pack/
__init__.py  stuff.py  
bruno@bigb:~/Work/playground/imps$ cat pack/stuff.py
class Foo(object):
    pass
bruno@bigb:~/Work/playground/imps$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
>>> import importlib
>>> module = importlib.import_module("pack.stuff")
>>> module
<module 'pack.stuff' from 'pack/stuff.pyc'>
>>> # now you can either use getattr() or directly refer to `module.Foo`: 
>>> cls = getattr(module, "Foo")
>>> cls
<class 'pack.stuff.Foo'>
>>> module.Foo
<class 'pack.stuff.Foo'>
>>> module.Foo is cls
True
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • it loads the module but when I run the module this way it gets error inside the module whereas when I load the module normally using from Class_mmm import * it works just fine. – Matt S Feb 10 '16 at 16:24
  • "star imports" (`from module import *`) are BAD, one should never use them in production code (really, been here done that etc) - consider this "feature" as just a handy shortcut when trying out things in the Python shell -, so just do what you should have done anyway: either use the qualified path (ie `module.Foo` instead of `Foo`) or explicitely bind the names in your namespace (ie: `Foo = module.Foo`) – bruno desthuilliers Feb 11 '16 at 10:10