1

I'm trying to run a script that: 1. parses an xml file, that contains an arbitary id (say xml001) 2. generates a new module (say xml001.py) 3. imports the generated module and evaluate a function contained within it.

As I don't know the generated module name in advance, I'm not sure how I can import it!

This code does not work....

modulefile = something + ruleid + '.py'
writer = open(modulefile,'wb')
writer.write(code and sh1t)
....
....

modulename = replace(modulefile,'.py','')

eval('import %s as gc'%modulename)
# import ruleid as gc

result = gc.evaluateRule(thing)

Gets an error message of

Traceback (most recent call last):
  File "C:\Stuff\roughwork4.py", line 152, in <module>
    eval('import %s as gc'%modulename)
  File "<string>", line 1
    import booleanExpressionA12345 as gc
         ^
SyntaxError: invalid syntax

Nor this where I've replaced the eval() bit (taken from here)...

modulename = open(modulefile)
gc = importCode(modulename,'gc')

Error message:

Traceback (most recent call last):
  File "C:\Stuff\roughwork4.py", line 161, in <module>
    result = gc.evaluateRule(rowvalueslist)
AttributeError: 'module' object has no attribute 'evaluateRule'

I appreciate any help! ps running on windows7, using eclipse ide....

AlanF
  • 25
  • 4
  • sorry, can't follow that one Martijn – AlanF Apr 30 '14 at 09:05
  • The duplicate tells you how to import a module dynamically; if your imported module doesn't actually *have* an `evaluateRule()` callable that'd be a different problem. You didn't tell us anything about how your modules are generated, so that's not something we can help with. – Martijn Pieters Apr 30 '14 at 09:07
  • Thanks Martijn, I tried `importlib` as the duplicate suggested and it worked... – AlanF Apr 30 '14 at 09:13

0 Answers0