On this page
encodings
Prev: email | Up: stdlib_module | Next: ensurepip
Rel: stdlib_module
Ref: docs.python.org — encodings
Tags: #public #python3 #stdlib
Import
import encodings
Overview
Standard-library package containing Python's built-in text and binary codec implementations (utf_8, latin_1, ascii, cp1252, idna, punycode, etc.) and encodings.normalize_encoding(). You rarely import encodings directly—codecs and str.encode()/bytes.decode() load modules from encodings automatically on demand.
Quick Start
import encodings
assert encodings.normalize_encoding("UTF-8") == "utf_8"
assert encodings.normalize_encoding("iso-8859-1") == "iso_8859_1"
assert encodings.search_function("utf-8") is not None
Implementation Walkthrough
1. Listing All Standard Codec Aliases
encodings.aliases.aliases maps canonical encoding aliases (like "latin1", "u8", "cp1252") to their module names under encodings:
import encodings.aliases
assert encodings.aliases.aliases["u8"] == "utf_8"
Gotchas & Boundaries
- Use codecs for public APIs: Prefer
codecs.lookup(name)overencodings.search_function(name)becausecodecs.lookup()also checks custom codecs registered viacodecs.register(). - Bootstrap requirement: Python's interpreter imports
encodingsduring startup; breakingPYTHONHOMEorsys.pathcommonly surfaces asFatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding.
Common Functions
- encodings.normalize_encoding() —
normalize_encoding(encoding)— Normalize an encoding name. - encodings.search_function() —
search_function(encoding)—encodings.search_function()function.
Classes & Objects
- encodings.CodecRegistryError —
encodings.CodecRegistryErrorclass. (Methods:.add_note(),.with_traceback())
Linked from
Related notes
- tuts/python3/stdlib_module/email Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/encodings/CodecRegistryError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/encodings/normalize_encoding Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/encodings/search_function Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ensurepip Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/codecs Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib