Package: eight
Eight is a Python module that provides a minimalist compatibility layer between Python 3 and 2. Eight lets you write
code for Python 3.3+ while providing limited compatibility with Python 2.7 with no code changes.  Eight is inspired bysix,nine, andpython-future, but provides better internationalization (i18n) support, is more
lightweight, easier to use, and unambiguously biased toward Python 3 code: if you remove eight from your code, it will
continue to function exactly as it did with eight on Python 3.To write code for Python 3 that is portable to Python 2, you may also want to read Armin Ronacher’s excellentPython 3
porting guide, as well as the officialporting guide.Writingfrom eight import *in your code is a no-op in Python 3. In Python 2, it binds a bunch of Python 3 names to
their Python 2 equivalents. Also, if you need to import a module or module member that was renamed in Python 3, writingfrom eight import <module>will do the right thing (equivalent toimport <module>on Python 3 andimport <old_name> as <module>on Python 2). Finally, eight can optionally wrap your standard streams and environment variable
I/O to use text, not bytes (see below).Installationpip install eightSynopsisfromeightimport*fromeightimportqueuefromeight.collectionsimportUserList,dequeIf you useprint, division, non-ASCII literals, or relative imports, you should also add thisfuture importat the top of each source file:from__future__import(print_function,division,unicode_literals,absolute_import)Wrapping stdioEight provides wrappers forsys.stdin,sys.stdout, andsys.stderrto make them (and methods that use them)
behave like they do on Python 3. Specifically, in Python 3 these streams accept text data, and their.bufferattributes
refer to the underlying streams that accept bytes. Eight uses theiomodule
to do the same for you, but subclasses the TextIOWrapper class forsys.stdoutandsys.stderrto coerce non-unicode
input to unicode on Python 2 (otherwise, because of the Python 2 semantics, things like exception printing cease to work).To enable stdio wrapping, use the following:importeighteight.wrap_stdio()To revert the effects of this on any of the streams, use the detach method, e.g.sys.stdin = sys.stdin.detach()(but
remember to condition this oneight.USING_PYTHON2). See theio module documentationfor more information.Decoding command-line argumentsEight provides a utility function to decode the contents ofsys.argvon Python 2 (as Python 3 does). It usessys.stdin.encodingas the encoding to do so:importeighteight.decode_command_line_args()The call todecode_command_line_args()replacessys.argvwith its decoded contents and returns the new contents.
On Python 3, the call is a no-op (it returnssys.argvand leaves it intact).Wrapping environment variable getters and settersEight provides utility wrappers to help bring Python 2 environment variable access and assignment in line with Python
3: encode the input toos.putenv(which is used for statements likeos.environ[x] = y) and decode the output ofos.getenv(used forx = os.environ[y]). Usewrap_os_environ_io()to monkey-patch these wrappers into theosmodule:importeighteight.wrap_os_environ_io()On Python 3, the call is a no-op.Selecting from the buffetYou can see whatfrom eight import *will do by runningIPythonand typingimport eight, theneight.<TAB>. Here is a full list of what’s available:asciibyteschrfilterhexinputintmapoctopenrangeroundstrsuperzipYou can import these symbols by listing them explicitly. If for any reason you see an issue with importing them all (which
is recommended), you can of course import a subset.In addition to names imported byfrom eight import *, the following modules are available and should be imported by
name usingfrom eight import <name>when needed:queue(old name:Queue)builtins(old name:__builtin__)copyreg(old name:copy_reg)configparser(old name:ConfigParser)reprlib(old name:repr)winreg(old name:_winreg)_thread(old name:thread)_dummy_thread(old name:dummy_thread)The following modules have attributes which resided elsewhere in Python 2: TODOAcknowledgmentsPython-futurefor doing a bunch of heavy lifting on backports of
Python 3 features.LinksProject home page (GitHub)Documentation (Read the Docs)Package distribution (PyPI)BugsPlease report bugs, issues, feature requests, etc. onGitHub.LicenseLicensed under the terms of theApache License, Version 2.0.