Generated code that is exec-ed (to simulate import) cannot import os.path??

Peter Otten __peter__ at web.de
Fri Nov 28 07:11:08 EST 2003


Irmen de Jong wrote:

> Hello,
> I don't understand why the following doesn't work.
> What I want to do is dynamically import some generated
> Python code and I'm doing this using compile and exec'ing
> it in the dict of a new empty module object.
> That works okay, but as soon as the generated code
> tries do perform certain imports, it fails!
> Certain other imports succeed. Consider this example code:

[...]

> What's going on? Why can't it find os.path?

My trial and error findings (Python 2.3.2 on Linux):

Python gets confused by the module name "generated.testmodule", when
a "generated" package does not exist; it seems to look for
"generated.posixpath" when it should for "os.posixpath" during the import
of the os module.

Two fixes are possible:

(1) Change

newmod = imp.new_module('generated.testmodule')

to, e. g.

newmod = imp.new_module('generated_testmodule')


(2) Create a dummy "generated" module and insert it into sys.modules:

import imp, sys

source="""
print 'importing random'
import random
print 'importing os'
import os
print 'bye!'

def getName():
     return 'Hello there'
"""

sys.modules["generated"] = imp.new_module("generated")

newmod = imp.new_module('generated.testmodule')
code=compile(source,'<generated code>','exec')
print 'got code...'
exec code in newmod.__dict__
print 'done, newmod.getname(): ',newmod.getName()

While this works, a helpful error message would be nice when an intermediate
package is not found. Unfortunately I was not able to track down the actual
point of failure yet.

Peter




More information about the Python-list mailing list