Python-Future

1.0.0 · maintenance · verified Sat Mar 28

future is the missing compatibility layer between Python 2 and Python 3. It allows you to use a single, clean Python 3.x-compatible codebase to support both Python 2 and Python 3 with minimal overhead. It provides `future` and `past` packages with backports and forward ports of features from Python 3 and 2, and includes `futurize` and `pasteurize` scripts for automated code conversion. The latest version is 1.0.0, and while feature development is complete, it supports Python 3.12.

Warnings

Install

Imports

Quickstart

To write new code compatible with both Python 2 (2.6+) and Python 3 (3.3+), start each module with the essential `__future__` imports and `from builtins import *`. Then call `install_aliases()` to ensure standard library modules are correctly mapped. This allows writing predominantly Python 3-style code that runs on both versions.

from __future__ import (absolute_import, division, print_function, unicode_literals)
from builtins import *
from future.standard_library import install_aliases
install_aliases()

# Now write Python 3 code that runs on both Py2 and Py3
print("Hello, world!")

def divide(a, b):
    return a / b # Will use float division even on Python 2

my_dict = {'a': 1, 'b': 2}
print(list(my_dict.keys())) # dict.keys() returns a view (iterable) on Py2 with future installed

# Example of Python 3 library name working on Py2
import urllib.request
print(urllib.request.urlopen('http://example.com').getcode())

view raw JSON →