Future F-strings
Future F-strings is a library that backported f-string (formatted string literal) syntax to Python versions prior to 3.6. It enables developers to use f-strings in Python 2.7, and Python 3.3-3.5 environments by transforming the source code. The current version is 1.2.0, but the library is now deprecated as its target Python versions have reached end-of-life and MicroPython, a common target for its `rewrite` functionality, now supports f-strings natively.
Warnings
- deprecated The `future-fstrings` library is officially deprecated by its maintainer. Python 2 and Python 3.5 (the target versions for runtime f-string support) have reached their end-of-life. Furthermore, MicroPython, a common target for its source transformation capabilities, now includes native f-string support. The library is no longer needed for its original purpose.
- gotcha The automatic codec registration via a `.pth` file may not work in certain deployment environments (e.g., AWS Lambda, or when `PYTHONPATH` is used instead of truly installed packages). In such cases, the `future_fstrings.register()` function must be called explicitly before importing any modules that use f-strings.
- gotcha When using the encoding cookie, the entire file is decoded and rewritten at once. While generally robust, complex or very large files *could* theoretically encounter performance issues during the initial load due to this on-the-fly source manipulation.
Install
-
pip install future-fstrings -
pip install future-fstrings[rewrite]
Imports
- Encoding Cookie
# -*- coding: future_fstrings -*- # Your f-string code here
- future_fstrings.register()
import future_fstrings future_fstrings.register()
Quickstart
# -*- coding: future_fstrings -*-
import os
name = os.environ.get('USER_NAME', 'world')
message = f'Hello {name}!'
print(message)
# To demonstrate the CLI transformation (requires future-fstrings[rewrite])
# Save the above code as 'my_app.py'
# Run in your shell:
# future-fstrings-show my_app.py
# This will output the Python2/3.5 compatible code (e.g., using .format())