Take this main.py:
from __future__ import print_function
from sub import print
print("hello, world")
and this sub.py:
from __future__ import print_function
def print(*args, **kwargs):
return __builtins__.print(*args, **kwargs)
Using Python 2.7.9, run main.py
and you get:
Traceback (most recent call last):
File "main.py", line 5, in <module>
print("hello, world")
File "/Users/ien/Studio/songifier/sub.py", line 4, in print
return __builtins__.print(*args, **kwargs)
AttributeError: 'dict' object has no attribute 'print'
Why and how to make this work?
NOTE: This is an artificial example to isolate the problem, which has arisen in a logging context, where the print
function sometimes does some fancy logging, and other times wants to just call the built-in print function.