ukpostcodeparser
raw JSON → 1.1.2 verified Fri May 01 auth: no python
A Python library for parsing and validating UK postcodes. Supports all common format types including inward/outward codes, GiroBank, BFPO, and special postcodes. Version 1.1.2 is current; the library is stable and receives infrequent updates.
pip install ukpostcodeparser Common errors
error ImportError: No module named 'ukpostcodeparser' ↓
cause Library not installed or installed in a different environment.
fix
Run
pip install ukpostcodeparser in the correct Python environment. error AttributeError: module 'ukpostcodeparser' has no attribute 'validate' ↓
cause Attempting to call a non-existent function named `validate` that may have existed in an older version or never existed.
fix
Use
parse_uk_postcode instead of validate. Check return value for None to indicate invalid postcode. error TypeError: parse_uk_postcode() missing 1 required positional argument: 'postcode' ↓
cause Calling the function without passing a postcode string.
fix
Pass a string argument, e.g.,
parse_uk_postcode('M1 1AE'). Warnings
deprecated The function `validate_uk_postcode` is deprecated in version 1.1.0+; use `parse_uk_postcode` instead and check if it returns None. ↓
fix Replace `validate_uk_postcode(p)` with `parse_uk_postcode(p) is not None`.
gotcha The function `parse_uk_postcode` accepts formatted postcode strings with or without space (e.g., 'SW1A1AA' and 'SW1A 1AA' both work). However, output always contains a space in the postcode field. Input validation is permissive: non-standard formats may be accepted as valid. ↓
fix Test with your expected input formats and validate using the parsed components if strict adherence to a specific format is needed.
gotcha This library does not validate that a postcode exists in the Royal Mail database; it only checks structural validity. It will accept some patterns that are structurally valid but not actually assigned. ↓
fix Use the parsed postcode with an external validation service (e.g., Ordnance Survey API) for existence checks.
Imports
- parse_uk_postcode
from ukpostcodeparser import parse_uk_postcode - parse_gb_postcode
from ukpostcodeparser import parse_gb_postcode
Quickstart
from ukpostcodeparser import parse_uk_postcode
postcode = parse_uk_postcode('SW1A 1AA')
if postcode:
print(postcode) # ParsedPostcode(outward='SW1A', inward='1AA', postcode='SW1A 1AA')
else:
print('Invalid postcode')