ImportError: No module named builtins

I am porting my python application from python 2 to python 3. As python-3 provides utility 2to3 which converts python-2 code to python-3. import builtins giving error as

ImportError: No module named builtins

Any idea to fix this issue?


Solution 1:

Solved a similar error in a separate situation by installing the package future.

sudo pip install future

It's not clear if your error occurs when running 2to3 or when trying to run the resulting code. If it is when running 2to3 it is likely because it's actually using python2 (the default) and thus if you haven't installed the future, builtins will be missing. Similarly if you are trying to run the resulting code with python2 the same error might occur.

Solution 2:

The 2to3 tool generates code compatible with Python 3-only.

You're probably seeing that because you're running the converted code in Python 2.

If you want your code to be compatible with Python 2 and 3, you can do this instead:

try:
    import builtins
except ImportError:
    import __builtin__ as builtins