Python 2 and 3 compatibility utilities
—
The six.moves module provides unified access to standard library modules and functions that were moved or renamed between Python 2 and 3. It supports 70+ relocated imports through a lazy loading system that dynamically maps old Python 2 locations to new Python 3 locations.
The central module object that provides access to all relocated imports.
moves: ModuleType # Main moves module providing access to relocated importsUsage Examples:
import six
# Access relocated modules
from six.moves import urllib
from six.moves import configparser
from six.moves import queue
# Use as module attributes
http_client = six.moves.http_client
builtins_module = six.moves.builtinsFunctions to add or remove items from the moves module at runtime.
def add_move(item: MovedAttribute | MovedModule) -> None
"""Add an item to six.moves at runtime."""
def remove_move(name: str) -> None
"""Remove an item from six.moves by name."""Usage Examples:
import six
# Add a custom moved attribute
custom_move = six.MovedAttribute("custom_name", "old.module", "new.module", "old_attr", "new_attr")
six.add_move(custom_move)
# Remove a moved attribute
six.remove_move("custom_name")Core standard library modules that were renamed or reorganized:
# Import examples
from six.moves import builtins # __builtin__ → builtins
from six.moves import configparser # ConfigParser → configparser
from six.moves import copyreg # copy_reg → copyreg
from six.moves import queue # Queue → queue
from six.moves import socketserver # SocketServer → socketserver
from six.moves import xmlrpc_client # xmlrpclib → xmlrpc.client
from six.moves import xmlrpc_server # SimpleXMLRPCServer → xmlrpc.serverHTTP-related modules that were reorganized in Python 3:
from six.moves import http_client # httplib → http.client
from six.moves import http_cookies # Cookie → http.cookies
from six.moves import http_cookiejar # cookielib → http.cookiejarComplete urllib package with all submodules:
from six.moves import urllib # Complete urllib package
from six.moves.urllib import parse # urlparse → urllib.parse
from six.moves.urllib import request # urllib2 → urllib.request
from six.moves.urllib import error # urllib2 → urllib.error
from six.moves.urllib import response # urllib2 → urllib.response
from six.moves.urllib.parse import urlparse, urljoin, quote
from six.moves.urllib.request import urlopen, RequestComplete Tkinter GUI framework modules:
from six.moves import tkinter # Tkinter → tkinter
from six.moves import tkinter_dialog # tkSimpleDialog → tkinter.simpledialog
from six.moves import tkinter_filedialog # tkFileDialog → tkinter.filedialog
from six.moves import tkinter_scrolledtext # ScrolledText → tkinter.scrolledtext
from six.moves import tkinter_simpledialog # tkSimpleDialog → tkinter.simpledialog
from six.moves import tkinter_tix # Tix → tkinter.tix
from six.moves import tkinter_ttk # ttk → tkinter.ttk
from six.moves import tkinter_constants # Tkconstants → tkinter.constants
from six.moves import tkinter_dnd2 # Tkdnd → tkinter.dnd
from six.moves import tkinter_tkfiledialog # tkFileDialog → tkinter.filedialog
from six.moves import tkinter_tksimpledialog # tkSimpleDialog → tkinter.simpledialog
from six.moves import tkinter_tkcolorchooser # tkColorChooser → tkinter.colorchooser
from six.moves import tkinter_tkcommonDialog # tkCommonDialog → tkinter.commondialog
from six.moves import tkinter_messagebox # tkMessageBox → tkinter.messagebox
from six.moves import tkinter_font # tkFont → tkinter.fontEmail MIME handling modules:
from six.moves import email_mime_base # email.MIMEBase → email.mime.base
from six.moves import email_mime_image # email.MIMEImage → email.mime.image
from six.moves import email_mime_multipart # email.MIMEMultipart → email.mime.multipart
from six.moves import email_mime_nonmultipart # email.MIMENonMultipart → email.mime.nonmultipart
from six.moves import email_mime_text # email.MIMEText → email.mime.textIndividual functions and types that moved:
from six.moves import filter # Built-in filter (returns iterator in PY3)
from six.moves import input # raw_input → input
from six.moves import map # Built-in map (returns iterator in PY3)
from six.moves import range # xrange → range
from six.moves import zip # izip → zip
from six.moves import zip_longest # izip_longest → zip_longest
from six.moves import reduce # Built-in reduce moved to functoolsVarious utility functions and classes:
from six.moves import cPickle # cPickle → pickle (C implementation)
from six.moves import reload_module # reload built-in → importlib.reload
from six.moves import shlex_quote # pipes.quote → shlex.quote
from six.moves import UserDict # UserDict → collections.UserDict
from six.moves import UserList # UserList → collections.UserList
from six.moves import UserString # UserString → collections.UserString
from six.moves import getcwd # os.getcwd
from six.moves import getcwdb # os.getcwdbDescriptor class for individual moved attributes within modules.
class MovedAttribute:
"""Descriptor for moved attributes."""
def __init__(
self,
name: str,
old_mod: str,
new_mod: str,
old_attr: str | None = None,
new_attr: str | None = None
)Parameters:
name: Name to use in six.movesold_mod: Python 2 module namenew_mod: Python 3 module nameold_attr: Python 2 attribute name (defaults to name)new_attr: Python 3 attribute name (defaults to name)Descriptor class for entire moved modules.
class MovedModule:
"""Descriptor for moved modules."""
def __init__(self, name: str, old: str, new: str | None = None)Parameters:
name: Name to use in six.movesold: Python 2 module namenew: Python 3 module name (defaults to old)Specialized lazy loader classes for urllib submodules:
class Module_six_moves_urllib_parse # Lazy loader for urllib.parse
class Module_six_moves_urllib_error # Lazy loader for urllib.error
class Module_six_moves_urllib_request # Lazy loader for urllib.request
class Module_six_moves_urllib_response # Lazy loader for urllib.response
class Module_six_moves_urllib_robotparser # Lazy loader for urllib.robotparser
class Module_six_moves_urllib # Lazy loader for complete urllib packageThese classes provide the implementation for the urllib submodule lazy loading system, allowing fine-grained access to urllib functionality across Python versions.
# Standard library imports
import six
from six.moves import urllib, configparser, queue
# HTTP operations
from six.moves.urllib.request import urlopen
from six.moves.urllib.parse import urljoin, urlparse
response = urlopen('http://example.com')
parsed = urlparse('http://example.com/path')
# Configuration parsing
config = configparser.ConfigParser()
# Queue operations
q = queue.Queue()
# Built-in function usage
from six.moves import filter, map, zip
result = list(filter(lambda x: x > 0, [-1, 0, 1, 2]))
mapped = list(map(str, [1, 2, 3]))
pairs = list(zip([1, 2, 3], ['a', 'b', 'c']))Install with Tessl CLI
npx tessl i tessl/pypi-six