Standard Library Reorganization¶
The standard library has been reorganized for Python 3.
Renamed Modules¶
- Fixer:
python-modernize -wnf libmodernize.fixes.fix_imports_six
- Prevalence: Common
Many modules were simply renamed, usually to unify file naming conventions
(e.g. ConfigParser
to configparser
) or to consolidate related
modules in a namespace (e.g. tkFont
to tkinter.font
).
The Compatibility library: six library includes six.moves
, a pseudo-package that exposes
moved modules under names that work in both Python 2 and 3.
For example, instead of:
from ConfigParser import ConfigParser
you should import from six.moves
:
from six.moves.configparser import ConfigParser
A list of all renamed modules is included in six documentation.
The recommended fixer will automatically change imports to use six.moves
.
Removed modules¶
- Fixer: None
- Prevalence: Uncommon
Some modules have been removed entirely.
Usually, these modules were supplanted by better alternatives
(e.g. mimetools
by email
),
specific to now-unsupported operating systems (e.g. fl
),
or known to be broken (e.g. Bastion
).
Lennart Regebro compiled a list of these modules in the book “Supporting Python 3”, which is available online.
If your code uses any of the removed modules, check the Python 2 documentation of the specific module for recommended replacements.
The urllib
modules¶
- Fixer: None
- Prevalence: Common
The urllib
, urllib2
and urlparse
modules were
reorganized more heavily, with individual functions and classes redistributed to
submodules of Python 3’s urllib
: urllib.parse
, urllib.error
,
urllib.request
, and urllib.response
.
These functions are included in six.moves
, and the six documentation
has details on what moved where.
Use this information to adjust your code.
The fix_imports_six
fixer recommended above does not handle all urllib
moves, so manual changes may be necessary.
The string
module¶
- Fixer: None
- Prevalence: Rare
In Python 2, the string
module included functions that mirrored str
methods, such as string.lower()
and string.join()
that mirror str.lower()
and str.join()
.
These have been deprecated since Python 2.4, and they are removed in Python 3.
Convert all uses of these functions to string methods.
For example, this code:
import string
products = ['widget', 'thingy', 'whatchamacallit']
print string.join(products, sep=', ')
should be replaced with:
products = ['widget', 'thingy', 'whatchamacallit']
print(', '.join(products))
The Automated fixer: python-modernize tool doesn’t provide an automated fixer for these changes.