'python-future' standard import not working

478 views
Skip to first unread message

Robert Pollak

unread,
Mar 26, 2015, 5:05:24 AM3/26/15
to winp...@googlegroups.com
Hello,

for a current coding task, I had to go back to Python 2, because of OpenCV's incomplete Python 3 support. I am using WinPython-64bit-2.7.9.3.exe.

In order to stay fully compatible to Python 3, I want to use the standard imports [0] of the 'python-future' package:


from __future__ import (absolute_import, division, print_function, unicode_literals)

from builtins import *


However, this gives me:

ImportError: No module named builtins

How can I make this work?

My first test case is to use Python-2's `raw_input` via its Python 3 name `input`.

Best regards,
Robert


[0] http://python-future.org/quickstart.html#if-you-are-writing-code-from-scratch

Yuxiang Wang

unread,
Mar 26, 2015, 12:37:14 PM3/26/15
to winp...@googlegroups.com
Hi Robert,

You need to install a package called `future`. Here are the steps to fix this problem:

1) Open WinPython Command Prompt.exe under your WinPython folder;
2) Run `pip install future` (without the '`' sign);
3) Done.

You can now use `from builtins import *`.

Shawn

SixString

unread,
Mar 26, 2015, 4:40:35 PM3/26/15
to winp...@googlegroups.com
Generally, I found that I don't need to import the built-in stuff to use it.  The exception is when I wanted to redefine a built-in function yet still reference the original.  Here is an example, which supports both Python 2 and Python 3:

import sys
if sys.version_info[0] < 3:

    import __builtin__ as builtins

else:

    import builtins


def len(in_array):

    # Purposely redefined built-in len(): pylint: disable=W0622

    if np.ndim(in_array) == 0:

        return 1

    else:

        return builtins.len(in_array)


Reply all
Reply to author
Forward
0 new messages