On Fri, Apr 3, 2009 at 11:35 AM, Anand Chitipothu <anandol...@gmail.com>wrote:
> 2009/4/3 Anand Chitipothu <anandol...@gmail.com>: > > I'm noticed a strange Import error in some web.py template. I nailed > > down the problem to the following python code. > > > > __builtins__ = {} > > > > import datetime > > now = datetime.datetime.utcnow() > > print now.strftime("%m %Y") > > > > It fails with the following error. > > > > Traceback (most recent call last): > > File "foo.py", line 6, in <module> > > print now.strftime("%m %Y") > > KeyError: '__import__' > > > > Any experts around for explaining this behavior? > > Another strange behavior: > > when run as script, it fails at the last line and when run tried in > interpreter, it fails at the import. When you import a module on the interpreter prompt, it is done using the builtin function __import__. So here you are setting __builtins__ dictionary (it is actually a module, not a dict, so this itself is strange) which removes all builtin function bindings. Hence when you are trying to import in interpreter prompt, it looks around for __import__ from __builtins__ module, does not find it and gives the error. I can't explain what is happening when it is run as a module. Perhaps here the import is not a simple __import__ , and hence it works - anyway that needs more explanation. > > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > http://mail.python.org/mailman/listinfo/bangpypers > -- -Anand
_______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers