{"id":489,"library":"future","title":"Python-Future","description":"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.","status":"maintenance","version":"1.0.0","language":"python","source_language":"en","source_url":"https://github.com/PythonCharmers/python-future","tags":["python2","python3","compatibility","migration","backport","futurize","pasteurize","six","2to3"],"install":[{"cmd":"pip install future","lang":"bash","label":"Install stable version"},{"cmd":"pip install future  # Requires Python >=2.6, !=3.0.*, !=3.1.*, !=3.2.*","lang":"bash","label":"Python compatibility"}],"dependencies":[],"imports":[{"note":"While `future.builtins` exists, the recommended pattern for Python 2/3 compatibility is `from builtins import ...` as it directly maps to Python 3 builtins on Py2 and has no effect on Py3. The wildcard `*` is shown in some examples for brevity but explicit imports are generally preferred.","wrong":"from future.builtins import *","symbol":"Builtins","correct":"from builtins import (bytes, dict, int, list, object, range, str,\n                       ascii, chr, hex, input, next, oct, open, pow,\n                       round, super, filter, map, zip)"},{"note":"This function makes Python 3-style standard library imports (e.g., `urllib.parse`, `queue`) available on Python 2 by modifying `sys.modules` and other mechanisms.","wrong":"import urllib.parse  # Directly on Py2 without aliases","symbol":"StandardLibraryAliases","correct":"from future.standard_library import install_aliases\ninstall_aliases()"},{"note":"These are built-in Python future statements crucial for enabling Python 3 behavior in Python 2. `future`'s tools and runtime patches complement these.","symbol":"__future__ imports","correct":"from __future__ import (absolute_import, division, print_function, unicode_literals)"},{"note":"Provides Python 2-specific builtins (like `basestring` or `unicode` type) on Python 3 for modules that explicitly need them, typically during module-by-module migration.","symbol":"past.builtins","correct":"from past.builtins import basestring, unicode"}],"quickstart":{"code":"from __future__ import (absolute_import, division, print_function, unicode_literals)\nfrom builtins import *\nfrom future.standard_library import install_aliases\ninstall_aliases()\n\n# Now write Python 3 code that runs on both Py2 and Py3\nprint(\"Hello, world!\")\n\ndef divide(a, b):\n    return a / b # Will use float division even on Python 2\n\nmy_dict = {'a': 1, 'b': 2}\nprint(list(my_dict.keys())) # dict.keys() returns a view (iterable) on Py2 with future installed\n\n# Example of Python 3 library name working on Py2\nimport urllib.request\nprint(urllib.request.urlopen('http://example.com').getcode())","lang":"python","description":"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."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":"<1.0.0 (and general Python 2 usage)"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions, especially 1.0.0+"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions on Python 2"},{"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.","message":"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.","severity":"deprecated","affected_versions":"All versions"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions on Python 2.x"}],"env_vars":null,"search_vec":"'1.0.0':73 '2':12,34,58 '2to3':92 '3':15,25,37,56 '3.12':83 'allow':17 'autom':66 'backport':48,88 'clean':23 'code':67 'codebas':29 'compat':8,28,86 'complet':79 'convers':68 'develop':77 'featur':53,76 'forward':50 'futur':3,4,43,61,89 'includ':60 'latest':70 'layer':9 'migrat':87 'minim':39 'miss':7 'overhead':40 'packag':46 'past':45 'pasteur':63,90 'port':51 'provid':42 'python':2,11,14,24,33,36,55,82 'python-futur':1 'python2':84 'python3':85 'script':64 'singl':22 'six':91 'support':31,81 'use':20 'version':71 'x':27 'x-compat':26","created_at":"2026-03-28T15:15:48.439882+00:00","updated_at":"2026-04-16T15:12:46.288164+00:00","problems":[{"fix":"pip install future","cause":"The 'future' library is not installed in the Python environment where the code is being executed.","error":"ModuleNotFoundError: No module named 'future'"},{"fix":"pip install future","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.","error":"ImportError: No module named builtins"},{"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.","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.","error":"SyntaxError: future feature annotations is not defined"},{"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.","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.","error":"SyntaxError: future feature print_function is not defined"},{"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`.","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.","error":"SyntaxError: invalid syntax (with 'L' suffix on numbers)"}],"ecosystem":"pypi","meta_description":null,"install_score":80,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"1.0.0","cli_name":"futurize","cli_version":"1.0.0","type":"library","homepage":"https://python-future.org","github":"https://github.com/PythonCharmers/python-future","docs":null,"changelog":null,"pypi":"https://pypi.org/project/future/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":"verified"}}