F
Fuzzyman
Hello all,
I am messing with namespaces, so that code I exec thinks it is
executing in the __main__ module.
I have the following code :
import imp
import sys
# can't call the module '__main__' or 'new_module' returns the real one
module = imp.new_module('_')
namespace = module.__dict__
namespace['__name__'] = '__main__'
# next put things into the names
# e.g. :
namespace['variable'] = 3
sys.modules['__main__'] = module
print module
print namespace
import __main__
print __main__.__dict__['variable']
This code behaves differently when entered into an interactive
interpreter session. When run as a program you will see that module and
namespace have both become None !!
However the code works, but to get access to the namespace again I have
to import __main__. I just wondered why ?
All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
I am messing with namespaces, so that code I exec thinks it is
executing in the __main__ module.
I have the following code :
import imp
import sys
# can't call the module '__main__' or 'new_module' returns the real one
module = imp.new_module('_')
namespace = module.__dict__
namespace['__name__'] = '__main__'
# next put things into the names
# e.g. :
namespace['variable'] = 3
sys.modules['__main__'] = module
print module
print namespace
import __main__
print __main__.__dict__['variable']
This code behaves differently when entered into an interactive
interpreter session. When run as a program you will see that module and
namespace have both become None !!
However the code works, but to get access to the namespace again I have
to import __main__. I just wondered why ?
All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml