Reputation: 2882
I have a package structure as:
parent_package/
__init__.py
module/
__init__.py
In the parent __init__.py
I have:
from __future__ import division
print(3/2) # 1.5
However, when I tried to reuse the import in its child, division does not take effect. In module's __init__.py
:
from parent_package.__init__ import division
print(3/2) # 1!
Upvotes: 0
Views: 77
Reputation: 43326
You are misunderstanding how the __future__
module works. __future__
is a special module that is built into the python interpreter and changes how the interpreter parses and/or executes your code. In order for a __future__
import to have the desired effect, it must be of the form
from __future__ import <feature>
(See PEP 236 for the exact specification.)
However, in addition to the __future__
module that's built into the interpreter, __future__
is also a real module in the standard library! The import from __future__ import divison
actually does two things: It enables the new division behavior, and it imports the feature specification from the real __future__
module. This is what you'll see if you take a look at the value of division
after the import:
>>> from __future__ import division
>>> division
_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
When you do from parent_package.__init__ import division
, you're simply importing this variable. But you're not enabling the new division behavior.
Upvotes: 2