mo-future
mo-future is a Python library designed to simplify Python 2/3 compatibility, aiming to be easier to use than `future` or `six`. It provides a flat namespace for common compatibility types and functions. The current version is 7.685.25166, with releases indicating active development.
Common errors
-
ImportError: cannot import name 'text' from 'future.utils'
cause Attempting to import compatibility symbols from `future.utils` when `mo-future` expects a direct import from its top-level module.fixChange the import statement to `from mo_future import text`. mo-future provides a flat namespace. -
AttributeError: module 'mo_future' has no attribute 'foo_py2_only'
cause Attempting to access a Python 2-specific compatibility shim that was removed from `mo-future` after December 2022.fixIf your project targets only Python 3, this attribute is no longer needed. If you require Python 2 compatibility, you may need to either use an older version of `mo-future` or implement Python 2-specific logic outside of `mo-future`. -
TypeError: 'map' object is not subscriptable
cause Treating the `map` function imported from `mo_future` as if it returns a list (Python 2 behavior) instead of an iterator (Python 3 behavior).fixExplicitly convert the `map` object to a list if you need to access elements by index or iterate over it multiple times: `list(my_map)`. The `map` from `mo_future` behaves like Python 3's `map` which returns an iterator.
Warnings
- breaking As of December 2022, mo-future no longer points to Python 2-specific modules. This means code relying on `mo-future` for direct Python 2 compatibility features might break.
- gotcha Users migrating from `python-future` or `six` might incorrectly assume `mo-future` maintains the same submodule structure (e.g., `future.utils`). `mo-future` promotes a flat namespace.
- gotcha The versioning scheme (e.g., 7.685.25166) appears highly granular or potentially generated. While this indicates active development, it might make tracking specific feature introductions or deprecations difficult without detailed release notes.
Install
-
pip install mo-future
Imports
- text
from future.utils import text
from mo_future import text
- bytes
from six import binary_type
from mo_future import bytes
Quickstart
from mo_future import text, bytes, range, map
# Example of using Python 3-like builtins compatible with Python 2 (if applicable)
print(type(text('hello')))
print(type(bytes(b'world')))
# range and map behave like their Python 3 counterparts (returning iterators)
my_range = range(5)
print(list(my_range))
my_map = map(lambda x: x * 2, [1, 2, 3])
print(list(my_map))
# A simple compatibility check (example, not actual mo_future feature)
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
print('Using urllib2 (Python 2 pattern)')
else:
print('Using urllib.request (Python 3 pattern)')