Python Reference ยท Practical Pages
Deprecated and removed Python features
Concept page on outdated Python: Python 2 leftovers, removed modules and functions, deprecated idioms such as % formatting and os.path chores, and their modern replacements.
Python is more than thirty years old, and old tutorials, Stack Overflow answers and legacy codebases still contain idioms that were replaced or removed. Some of them raise errors today; others still work but have better modern alternatives. This page lists the ones you are most likely to meet, with the replacement for each.
Python's compatibility policy is: a feature is first deprecated (it works but emits a DeprecationWarning, which is hidden by default outside __main__ and test runners), then removed in a later release. Run your tests with python -W error::DeprecationWarning or pytest -W error to find uses before an upgrade breaks them.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Python 2 leftovers
| Old | Modern |
|---|---|
print "x" |
print("x") |
raw_input() |
input() |
xrange() |
range() |
unicode, long, basestring |
str, int, str |
d.has_key(k), d.iteritems() |
k in d, d.items() |
except E, e: |
except E as e: |
raise E, "msg" |
raise E("msg") |
exec code |
exec(code) |
from __future__ import ... for Py3 features |
not needed (the imports still work) |
class Foo(object) |
class Foo: (the base object is implicit) |
super(Foo, self).__init__() |
super().__init__() |
Deprecated or removed in Python 3
| Old | Replacement |
|---|---|
distutils (removed 3.12) |
setuptools and pyproject.toml |
imp (removed 3.12) |
importlib |
asyncore, asynchat, smtpd (removed 3.12) |
asyncio |
cgi, telnetlib, pipes, crypt, imghdr, sndhdr (removed 3.13) |
third-party packages or email, html, subprocess |
datetime.utcnow(), utcfromtimestamp() |
datetime.now(UTC) |
typing.List, Dict, Tuple, Set |
list, dict, tuple, set |
typing.Optional[X], Union[X, Y] |
X | None, X | Y |
os.path string chores |
pathlib.Path |
os.system(cmd) |
subprocess.run([...], check=True) |
% formatting, str.format() for simple cases |
f-strings |
collections.Mapping etc. (removed 3.10) |
collections.abc.Mapping |
pkg_resources, setup.py install |
importlib.metadata, pip install . |
logging.warn(), assertEquals() |
logging.warning(), assertEqual() |
locale.getdefaultlocale() |
locale.getlocale() and friends |
from module import * |
explicit imports |
open(path) without encoding= |
open(path, encoding="utf-8") |
| Mutable default arguments | None plus a fresh object inside |
bare except: |
except Exception: or a specific class |
Usage notes
- The authoritative list for each release is the "What's New" document and the removal schedule in the language's deprecation PEPs. When upgrading, read both for the versions you skip.
DeprecationWarningis aimed at developers andFutureWarningat end users. Silence a specific warning withwarnings.filterwarnings, not by ignoring all warnings.- Features can be soft-deprecated: still fine to use but not recommended in new code (for example
typing.Text). - Third-party libraries deprecate APIs too. Pin versions in applications, and fix warnings before bumping major versions.
- Code that must run on several versions can check
sys.version_info >= (3, 12)or usetry: ... except ImportError. - The
2to3tool was removed in 3.13; usepython-modernize,pyupgradeorruff --select UPto update old syntax.
Related
- Warnings, sys, datetime
- Packaging and projects
- Tutorial lessons: Pythonic code, Logging and warnings, String formatting