Python 2 and Python 3 spell some module and class names differently, and pickles store
those names (GLOBAL, INST). The fix_imports option
of pickle translates them, using the tables in CPython’s
Lib/_compat_pickle.py
(main branch, read 2026-11-14) as used by
Lib/pickle.py.
The module’s own comment says the first table is a copy of
lib2to3.fixes.fix_imports.MAPPING.
Versions: the tables below are those of the main branch, with two entries added that CPython 3.13 and 3.14
(tags v3.13.0 and v3.14.0, and the 3.14 branch, read 2026-11-14) still have
and the main branch has dropped, together with the CGI server they name:
CGIHTTPServer → http.server and http.server.CGIHTTPRequestHandler ←
CGIHTTPServer.CGIHTTPRequestHandler. Each is marked “3.13 and 3.14 only”. In the other direction, the
main branch lists builtins.ImportCycleError among the names written as
exceptions.ImportError, which 3.13 and 3.14 do not. Nothing else differs between them.
When it applies
- Reading.
Unpickler(file, fix_imports=True)is the default. Infind_class(module, name), when the stream’s protocol (from itsPROTOopcode, or 0 if there is none) is below 3 andfix_importsis true: if(module, name)is inNAME_MAPPINGit is replaced by the pair found; otherwise ifmoduleis inIMPORT_MAPPINGthe module is replaced. The name mapping is tried first, so a full pair beats a module rule. The module is then imported and the name looked up. With protocol 3 or more, orfix_imports=False, names are used as written. The unpickler’sencodinganderrorsarguments (defaultASCII,strict), or'bytes', decide how Python 2 8-bit strings (STRING,BINSTRING,SHORT_BINSTRING) are read. - Writing.
Pickler(file, protocol, fix_imports=True)applies the opposite tables only when the protocol is below 3:(module, name)inREVERSE_NAME_MAPPINGis replaced; otherwise a module inREVERSE_IMPORT_MAPPINGis replaced, so that Python 2 can read the result. With protocol 3 or more nothing is changed. - The reverse tables are the inverse of the forward tables built from the first part of each, plus the extra entries listed under each table below. A built-in assertion checks that the inversion loses no entry.
IMPORT_MAPPING: module names
A Python 2 module name and its Python 3 replacement.
| Python 2 module | Python 3 module |
|---|---|
__builtin__ | builtins |
copy_reg | copyreg |
Queue | queue |
SocketServer | socketserver |
ConfigParser | configparser |
repr | reprlib |
tkFileDialog | tkinter.filedialog |
tkSimpleDialog | tkinter.simpledialog |
tkColorChooser | tkinter.colorchooser |
tkCommonDialog | tkinter.commondialog |
Dialog | tkinter.dialog |
Tkdnd | tkinter.dnd |
tkFont | tkinter.font |
tkMessageBox | tkinter.messagebox |
ScrolledText | tkinter.scrolledtext |
Tkconstants | tkinter.constants |
ttk | tkinter.ttk |
Tkinter | tkinter |
markupbase | _markupbase |
_winreg | winreg |
thread | _thread |
dummy_thread | _dummy_thread |
dbhash | dbm.bsd |
dumbdbm | dbm.dumb |
dbm | dbm.ndbm |
gdbm | dbm.gnu |
xmlrpclib | xmlrpc.client |
SimpleXMLRPCServer | xmlrpc.server |
httplib | http.client |
htmlentitydefs | html.entities |
HTMLParser | html.parser |
Cookie | http.cookies |
cookielib | http.cookiejar |
BaseHTTPServer | http.server |
test.test_support | test.support |
commands | subprocess |
urlparse | urllib.parse |
robotparser | urllib.robotparser |
urllib2 | urllib.request |
anydbm | dbm |
_abcoll | collections.abc |
These forward-only additions follow, “non-mutual” so they are not inverted (the comment says some are for broken pickles saved by old Python 3 versions):
| Python 2 module | Python 3 module |
|---|---|
cPickle | pickle |
_elementtree | xml.etree.ElementTree |
FileDialog | tkinter.filedialog |
SimpleDialog | tkinter.simpledialog |
DocXMLRPCServer | xmlrpc.server |
SimpleHTTPServer | http.server |
CGIHTTPServer (3.13 and 3.14 only) | http.server |
UserDict, UserList, UserString | collections |
whichdb | dbm |
StringIO, cStringIO | io |
Extra entries only in REVERSE_IMPORT_MAPPING (Python 3 to Python 2): _bz2 to bz2, _dbm to dbm, _functools to functools, _gdbm to gdbm, _pickle to pickle.
NAME_MAPPING: module and name pairs
Rename rules that are “easy to handle”; the module notes say the more complex cases (urllib, types) are ignored. They run before the module rules.
| Python 2 (module, name) | Python 3 (module, name) |
|---|---|
__builtin__.xrange | builtins.range |
__builtin__.reduce | functools.reduce |
__builtin__.intern | sys.intern |
__builtin__.unichr | builtins.chr |
__builtin__.unicode | builtins.str |
__builtin__.long | builtins.int |
__builtin__.basestring | builtins.str (forward only) |
itertools.izip | builtins.zip |
itertools.imap | builtins.map |
itertools.ifilter | builtins.filter |
itertools.ifilterfalse | itertools.filterfalse |
itertools.izip_longest | itertools.zip_longest |
UserDict.IterableUserDict | collections.UserDict |
UserDict.UserDict | collections.UserDict (forward only) |
UserList.UserList | collections.UserList |
UserString.UserString | collections.UserString |
whichdb.whichdb | dbm.whichdb |
_socket.fromfd | socket.fromfd |
socket._socketobject | socket.SocketType (forward only) |
_multiprocessing.Connection | multiprocessing.connection.Connection |
multiprocessing.process.Process | multiprocessing.context.Process |
multiprocessing.forking.Popen | multiprocessing.popen_fork.Popen |
urllib.ContentTooShortError | urllib.error.ContentTooShortError |
urllib.getproxies | urllib.request.getproxies |
urllib.pathname2url | urllib.request.pathname2url |
urllib.quote_plus | urllib.parse.quote_plus |
urllib.quote | urllib.parse.quote |
urllib.unquote_plus | urllib.parse.unquote_plus |
urllib.unquote | urllib.parse.unquote |
urllib.url2pathname | urllib.request.url2pathname |
urllib.urlcleanup | urllib.request.urlcleanup |
urllib.urlencode | urllib.parse.urlencode |
urllib.urlopen | urllib.request.urlopen |
urllib.urlretrieve | urllib.request.urlretrieve |
urllib2.HTTPError | urllib.error.HTTPError |
urllib2.URLError | urllib.error.URLError |
exceptions.StandardError | builtins.Exception (forward only) |
multiprocessing.AuthenticationError, BufferTooShort, ProcessError, TimeoutError | the same names in multiprocessing.context |
Python 2 exceptions. Each name below in module exceptions maps to the same name in builtins (WindowsError is added only where Python defines it): ArithmeticError, AssertionError, AttributeError, BaseException, BufferError, BytesWarning, DeprecationWarning, EOFError, EnvironmentError, Exception, FloatingPointError, FutureWarning, GeneratorExit, IOError, ImportError, ImportWarning, IndentationError, IndexError, KeyError, KeyboardInterrupt, LookupError, MemoryError, NameError, NotImplementedError, OSError, OverflowError, PendingDeprecationWarning, ReferenceError, RuntimeError, RuntimeWarning, StopIteration, SyntaxError, SyntaxWarning, SystemError, SystemExit, TabError, TypeError, UnboundLocalError, UnicodeDecodeError, UnicodeEncodeError, UnicodeError, UnicodeTranslateError, UnicodeWarning, UserWarning, ValueError, Warning, ZeroDivisionError.
Extra entries only in REVERSE_NAME_MAPPING (Python 3 to Python 2):
| Python 3 (module, name) | Python 2 (module, name) |
|---|---|
_functools.reduce | __builtin__.reduce |
tkinter.filedialog.FileDialog, LoadFileDialog, SaveFileDialog | the same names in FileDialog |
tkinter.simpledialog.SimpleDialog | SimpleDialog.SimpleDialog |
xmlrpc.server.ServerHTMLDoc, XMLRPCDocGenerator, DocXMLRPCRequestHandler, DocXMLRPCServer, DocCGIXMLRPCRequestHandler | the same names in DocXMLRPCServer |
http.server.SimpleHTTPRequestHandler | SimpleHTTPServer.SimpleHTTPRequestHandler |
http.server.CGIHTTPRequestHandler (3.13 and 3.14 only) | CGIHTTPServer.CGIHTTPRequestHandler |
_socket.socket | socket._socketobject |
builtins.BrokenPipeError, ChildProcessError, ConnectionAbortedError, ConnectionError, ConnectionRefusedError, ConnectionResetError, FileExistsError, FileNotFoundError, InterruptedError, IsADirectoryError, NotADirectoryError, PermissionError, ProcessLookupError, TimeoutError | exceptions.OSError |
builtins.ImportCycleError, ModuleNotFoundError | exceptions.ImportError |