Python-Future

raw JSON →
1.0.0 verified Tue May 12 auth: no python install: verified quickstart: verified maintenance

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.

pip install future
error ModuleNotFoundError: No module named 'future'
cause The 'future' library is not installed in the Python environment where the code is being executed.
fix
pip install future
error ImportError: No module named builtins
cause This error typically occurs in Python 2 when `from builtins import *` is used, but the 'future' library, which backports Python 3 builtins to Python 2, is not installed.
fix
pip install future
error SyntaxError: future feature annotations is not defined
cause The `from __future__ import annotations` syntax, introduced by PEP 563, is only available from Python 3.7 onwards. This error occurs when trying to use it in an older Python version.
fix
Upgrade your Python interpreter to version 3.7 or newer, or remove the from __future__ import annotations statement if it's not critical for older Python versions.
error SyntaxError: future feature print_function is not defined
cause This error occurs in Python 2 when `from __future__ import print_function` is used with a Python 2 version older than 2.6, as the print function feature was introduced in Python 2.6.
fix
Upgrade your Python 2 interpreter to version 2.6 or newer, or ensure the code using this future import is not run on older Python 2 versions.
error SyntaxError: invalid syntax (with 'L' suffix on numbers)
cause Python 2 used an 'L' suffix (e.g., `1234L`) to denote long integers. Python 3 merged 'int' and 'long' types, making the 'L' suffix invalid syntax.
fix
Remove the 'L' suffix from integer literals. The futurize script from the 'future' library can help automate this conversion across your codebase by running futurize -w your_script.py.
breaking Python 2 reached its end-of-life in 2020. While `python-future` facilitates compatibility, relying on it for actively maintained Python 2 codebases is increasingly risky due to lack of Python 2 security updates and ecosystem support.
fix Prioritize full migration to Python 3. `python-future` should be seen as a stepping stone or a compatibility layer for transitioning legacy code, not a long-term solution for Python 2 development.
gotcha The `python-future` project is declared 'done' and is not recommended for new Python 3 projects. While it receives critical compatibility updates (e.g., Python 3.12 support), its primary purpose as a Python 2/3 compatibility layer means new feature development is complete. It should not be adopted as a dependency for greenfield Python 3 development.
fix For new Python 3 projects, write idiomatic Python 3 code directly. If compatibility with multiple Python 3 versions is needed, leverage `__future__` imports (native to Python) and `six` if strictly necessary for very old Python 3 versions, but prefer modern Python features.
gotcha The `install_aliases()` function globally modifies `sys.modules` on Python 2 to map Python 3 standard library names. This can lead to unexpected behavior or conflicts if other libraries or custom import hooks make different assumptions about the module layout.
fix Use `install_aliases()` early in your application's startup. Be aware of potential conflicts, especially when mixing with other compatibility layers or dynamically loading modules. Consider explicit `future.moves` imports for specific items to limit global impact if issues arise.
deprecated The `past.translation` module, which offers experimental automatic translation of Python 2 modules to Python 3 upon import, is in 'alpha' status and may be unstable or imperfect. Relying on it for critical functionality is not recommended.
fix Prefer using `futurize` (for Py2 to Py2/3) or `pasteurize` (for Py3 to Py2/3) scripts for static code conversion. If `past.translation` is used, ensure thorough testing and be prepared for potential runtime issues.
gotcha HTTPS support with the `http`-based backports within `future` is limited on Python 2.x due to significant changes in SSL support in Python 3.x. Direct `http`-based backports might fail for HTTPS connections.
fix For HTTPS support on Python 2, especially within `urllib`, the documentation recommends using `from future.moves.urllib.request import urlopen` or using a dedicated HTTP library like `requests` that handles SSL/TLS more robustly.
pip install future # Requires Python >=2.6, !=3.0.*, !=3.1.*, !=3.2.*
python os / libc status wheel install import disk
3.10 alpine (musl) - - - -
3.10 alpine (musl) - - - -
3.10 slim (glibc) - - - -
3.10 slim (glibc) - - - -
3.11 alpine (musl) - - - -
3.11 alpine (musl) - - - -
3.11 slim (glibc) - - - -
3.11 slim (glibc) - - - -
3.12 alpine (musl) - - - -
3.12 alpine (musl) - - - -
3.12 slim (glibc) - - - -
3.12 slim (glibc) - - - -
3.13 alpine (musl) - - - -
3.13 alpine (musl) - - - -
3.13 slim (glibc) - - - -
3.13 slim (glibc) - - - -
3.9 alpine (musl) - - - -
3.9 alpine (musl) - - - -
3.9 slim (glibc) - - - -
3.9 slim (glibc) - - - -

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())