phonenumbers 9.0.16


pip install phonenumbers

  Latest version

Released: Oct 10, 2025

Project Links

Meta
Author: David Drysdale

Classifiers

Development Status
  • 5 - Production/Stable

Intended Audience
  • Developers

License
  • OSI Approved :: Apache Software License

Operating System
  • OS Independent

Topic
  • Communications :: Telephony

Programming Language
  • Python :: 2
  • Python :: 2.5
  • Python :: 2.6
  • Python :: 2.7
  • Python :: 3
  • Python :: 3.3
  • Python :: 3.4
  • Python :: 3.5
  • Python :: 3.6
  • Python :: 3.7
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: Implementation :: CPython
  • Python :: Implementation :: PyPy

phonenumbers Python Library

Coverage Status

This is a Python port of Google's libphonenumber library It supports Python 2.5-2.7 and Python 3.x (in the same codebase, with no 2to3 conversion needed).

Original Java code is Copyright (C) 2009-2015 The Libphonenumber Authors.

Release HISTORY, derived from upstream release notes.

Documentation

Installation

Install using pip with:

pip install phonenumbers

Example Usage

The main object that the library deals with is a PhoneNumber object. You can create this from a string representing a phone number using the parse function, but you also need to specify the country that the phone number is being dialled from (unless the number is in E.164 format, which is globally unique).

>>> import phonenumbers
>>> x = phonenumbers.parse("+442083661177", None)
>>> print(x)
Country Code: 44 National Number: 2083661177 Leading Zero: False
>>> type(x)
<class 'phonenumbers.phonenumber.PhoneNumber'>
>>> y = phonenumbers.parse("020 8366 1177", "GB")
>>> print(y)
Country Code: 44 National Number: 2083661177 Leading Zero: False
>>> x == y
True
>>> z = phonenumbers.parse("00 1 650 253 2222", "GB")  # as dialled from GB, not a GB number
>>> print(z)
Country Code: 1 National Number: 6502532222 Leading Zero(s): False

The PhoneNumber object that parse produces typically still needs to be validated, to check whether it's a possible number (e.g. it has the right number of digits) or a valid number (e.g. it's in an assigned exchange).

>>> z = phonenumbers.parse("+120012301", None)
>>> print(z)
Country Code: 1 National Number: 20012301 Leading Zero: False
>>> phonenumbers.is_possible_number(z)  # too few digits for USA
False
>>> phonenumbers.is_valid_number(z)
False
>>> z = phonenumbers.parse("+12001230101", None)
>>> print(z)
Country Code: 1 National Number: 2001230101 Leading Zero: False
>>> phonenumbers.is_possible_number(z)
True
>>> phonenumbers.is_valid_number(z)  # NPA 200 not used
False

The parse function will also fail completely (with a NumberParseException) on inputs that cannot be uniquely parsed, or that can't possibly be phone numbers.

>>> z = phonenumbers.parse("02081234567", None)  # no region, no + => unparseable
Traceback (most recent call last):
  File "phonenumbers/phonenumberutil.py", line 2350, in parse
    "Missing or invalid default region.")
phonenumbers.phonenumberutil.NumberParseException: (0) Missing or invalid default region.
>>> z = phonenumbers.parse("gibberish", None)
Traceback (most recent call last):
  File "phonenumbers/phonenumberutil.py", line 2344, in parse
    "The string supplied did not seem to be a phone number.")
phonenumbers.phonenumberutil.NumberParseException: (1) The string supplied did not seem to be a phone number.

Once you've got a phone number, a common task is to format it in a standardized format. There are a few formats available (under PhoneNumberFormat), and the format_number function does the formatting.

>>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.NATIONAL)
'020 8366 1177'
>>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
'+44 20 8366 1177'
>>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.E164)
'+442083661177'

If your application has a UI that allows the user to type in a phone number, it's nice to get the formatting applied as the user types. The AsYouTypeFormatter object allows this.

>>> formatter = phonenumbers.AsYouTypeFormatter("US")
>>> formatter.input_digit("6")
'6'
>>> formatter.input_digit("5")
'65'
>>> formatter.input_digit("0")
'650'
>>> formatter.input_digit("2")
'650 2'
>>> formatter.input_digit("5")
'650 25'
>>> formatter.input_digit("3")
'650 253'
>>> formatter.input_digit("2")
'650-2532'
>>> formatter.input_digit("2")
'(650) 253-22'
>>> formatter.input_digit("2")
'(650) 253-222'
>>> formatter.input_digit("2")
'(650) 253-2222'

Sometimes, you've got a larger block of text that may or may not have some phone numbers inside it. For this, the PhoneNumberMatcher object provides the relevant functionality; you can iterate over it to retrieve a sequence of PhoneNumberMatch objects. Each of these match objects holds a PhoneNumber object together with information about where the match occurred in the original string.

>>> text = "Call me at 510-748-8230 if it's before 9:30, or on 703-4800500 after 10am."
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
...     print(match)
...
PhoneNumberMatch [11,23) 510-748-8230
PhoneNumberMatch [51,62) 703-4800500
>>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
...     print(phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164))
...
+15107488230
+17034800500

You might want to get some information about the location that corresponds to a phone number. The geocoder.description_for_number does this, when possible.

>>> from phonenumbers import geocoder
>>> ch_number = phonenumbers.parse("0431234567", "CH")
>>> geocoder.description_for_number(ch_number, "de")
'Zürich'
>>> geocoder.description_for_number(ch_number, "en")
'Zurich'
>>> geocoder.description_for_number(ch_number, "fr")
'Zurich'
>>> geocoder.description_for_number(ch_number, "it")
'Zurigo'

For mobile numbers in some countries, you can also find out information about which carrier originally owned a phone number.

>>> from phonenumbers import carrier
>>> ro_number = phonenumbers.parse("+40721234567", "RO")
>>> carrier.name_for_number(ro_number, "en")
'Vodafone'

You might also be able to retrieve a list of time zone names that the number potentially belongs to.

>>> from phonenumbers import timezone
>>> gb_number = phonenumbers.parse("+447986123456", "GB")
>>> timezone.time_zones_for_number(gb_number)
('Atlantic/Reykjavik', 'Europe/London')

For more information about the other functionality available from the library, look in the unit tests or in the original libphonenumber project.

Memory Usage

The library includes a lot of metadata, potentially giving a significant memory overhead. There are two mechanisms for dealing with this.

  • The normal metadata (just over 2 MiB of generated Python code) for the core functionality of the library is loaded on-demand, on a region-by-region basis (i.e. the metadata for a region is only loaded on the first time it is needed).
  • Metadata for extended functionality is held in separate packages, which therefore need to be explicitly loaded separately. This affects:
    • The geocoding metadata (~19 MiB), which is held in phonenumbers.geocoder and used by the geocoding functions (geocoder.description_for_number, geocoder.description_for_valid_number or geocoder.country_name_for_number).
    • The carrier metadata (~1 MiB), which is held in phonenumbers.carrier and used by the mapping functions (carrier.name_for_number or carrier.name_for_valid_number).
    • The timezone metadata (~100 KiB), which is held in phonenumbers.timezone and used by the timezone functions (time_zones_for_number or time_zones_for_geographical_number).

The phonenumberslite version of the library does not include the geocoder, carrier and timezone packages, which can be useful if you have problems installing the main phonenumbers library due to space/memory limitations.

If you need to ensure that the metadata memory use is accounted for at start of day (i.e. that a subsequent on-demand load of metadata will not cause a pause or memory exhaustion):

  • Force-load the normal metadata by calling phonenumbers.PhoneMetadata.load_all().
  • Force-load the extended metadata by importing the appropriate packages (phonenumbers.geocoder, phonenumbers.carrier, phonenumbers.timezone).

Static Typing

The library includes a set of type stub files to support static type checking by library users. These stub files signal the types that should be used, and may also be of use in IDEs which have integrated type checking functionalities.

These files are written for Python 3, and as such type checking the library with these stubs on Python 2.5-2.7 is unsupported.

Project Layout

  • The python/ directory holds the Python code.
  • The resources/ directory is a copy of the resources/ directory from libphonenumber. This is not needed to run the Python code, but is needed when upstream changes to the master metadata need to be incorporated.
  • The tools/ directory holds the tools that are used to process upstream changes to the master metadata.
9.0.16 Oct 10, 2025
9.0.15 Sep 26, 2025
9.0.14 Sep 16, 2025
9.0.13 Aug 29, 2025
9.0.12 Aug 15, 2025
9.0.11 Aug 07, 2025
9.0.10 Jul 18, 2025
9.0.9 Jul 08, 2025
9.0.8 Jun 25, 2025
9.0.7 Jun 09, 2025
9.0.6 May 28, 2025
9.0.5 May 08, 2025
9.0.4 Apr 25, 2025
9.0.3 Apr 11, 2025
9.0.2 Mar 28, 2025
9.0.1 Mar 14, 2025
9.0.0 Mar 04, 2025
8.13.55 Feb 15, 2025
8.13.54 Jan 31, 2025
8.13.53 Jan 16, 2025
8.13.52 Dec 13, 2024
8.13.51 Dec 02, 2024
8.13.50 Nov 16, 2024
8.13.49 Nov 04, 2024
8.13.48 Oct 22, 2024
8.13.47 Oct 04, 2024
8.13.46 Sep 25, 2024
8.13.45 Sep 06, 2024
8.13.44 Aug 26, 2024
8.13.43 Aug 09, 2024
8.13.42 Jul 30, 2024
8.13.40 Jul 01, 2024
8.13.39 Jun 15, 2024
8.13.38 Jun 04, 2024
8.13.37 May 17, 2024
8.13.36 May 03, 2024
8.13.35 Apr 19, 2024
8.13.34 Apr 05, 2024
8.13.33 Mar 25, 2024
8.13.32 Mar 13, 2024
8.13.31 Feb 26, 2024
8.13.30 Feb 09, 2024
8.13.29 Jan 26, 2024
8.13.28 Jan 17, 2024
8.13.27 Dec 20, 2023
8.13.26 Nov 23, 2023
8.13.25 Nov 14, 2023
8.13.24 Oct 31, 2023
8.13.23 Oct 17, 2023
8.13.22 Sep 29, 2023
8.13.21 Sep 20, 2023
8.13.20 Sep 07, 2023
8.13.19 Aug 22, 2023
8.13.18 Aug 04, 2023
8.13.17 Jul 21, 2023
8.13.16 Jul 10, 2023
8.13.15 Jun 23, 2023
8.13.14 Jun 13, 2023
8.13.13 May 30, 2023
8.13.11 Apr 27, 2023
8.13.10 Apr 18, 2023
8.13.9 Apr 11, 2023
8.13.8 Mar 27, 2023
8.13.7 Mar 03, 2023
8.13.6 Feb 10, 2023
8.13.5 Jan 29, 2023
8.13.4 Jan 09, 2023
8.13.3 Dec 22, 2022
8.13.2 Dec 08, 2022
8.13.1 Nov 28, 2022
8.13.0 Nov 06, 2022
8.12.57 Oct 14, 2022
8.12.56 Sep 24, 2022
8.12.55 Sep 09, 2022
8.12.54 Aug 19, 2022
8.12.53 Aug 04, 2022
8.12.52 Jul 19, 2022
8.12.51 Jul 01, 2022
8.12.50 Jun 10, 2022
8.12.49 May 28, 2022
8.12.48 May 06, 2022
8.12.47 Apr 21, 2022
8.12.46 Apr 01, 2022
8.12.45 Mar 11, 2022
8.12.44 Feb 25, 2022
8.12.43 Feb 10, 2022
8.12.42 Jan 29, 2022
8.12.41 Jan 11, 2022
8.12.40 Dec 25, 2021
8.12.39 Dec 07, 2021
8.12.38 Nov 27, 2021
8.12.37 Nov 11, 2021
8.12.36 Oct 27, 2021
8.12.35 Oct 14, 2021
8.12.34 Oct 07, 2021
8.12.33 Sep 24, 2021
8.12.32 Sep 12, 2021
8.12.31 Aug 24, 2021
8.12.30 Aug 18, 2021
8.12.29 Aug 09, 2021
8.12.28 Jul 22, 2021
8.12.27 Jul 07, 2021
8.12.26 Jun 25, 2021
8.12.25 Jun 15, 2021
8.12.24 May 27, 2021
8.12.23 May 13, 2021
8.12.22 Apr 30, 2021
8.12.21 Apr 07, 2021
8.12.20 Mar 18, 2021
8.12.19 Mar 02, 2021
8.12.18 Feb 09, 2021
8.12.17 Jan 27, 2021
8.12.16 Jan 12, 2021
8.12.15 Dec 16, 2020
8.12.14 Dec 04, 2020
8.12.13 Nov 13, 2020
8.12.12 Nov 01, 2020
8.12.11 Oct 10, 2020
8.12.10 Sep 29, 2020
8.12.9 Sep 01, 2020
8.12.8 Aug 17, 2020
8.12.7 Jul 25, 2020
8.12.6 Jun 19, 2020
8.12.5 Jun 04, 2020
8.12.4 May 21, 2020
8.12.3 May 08, 2020
8.12.2 Apr 23, 2020
8.12.1 Mar 31, 2020
8.12.0 Mar 20, 2020
8.11.5 Mar 03, 2020
8.11.4 Feb 13, 2020
8.11.3 Feb 03, 2020
8.11.2 Jan 16, 2020
8.11.1 Dec 12, 2019
8.11.0 Nov 28, 2019
8.10.23 Nov 18, 2019
8.10.22 Oct 31, 2019
8.10.21 Oct 17, 2019
8.10.20 Oct 03, 2019
8.10.19 Sep 20, 2019
8.10.18 Sep 06, 2019
8.10.17 Aug 17, 2019
8.10.16 Aug 05, 2019
8.10.15 Jul 16, 2019
8.10.14 Jun 22, 2019
8.10.13 May 31, 2019
8.10.12 May 15, 2019
8.10.11 May 07, 2019
8.10.10 Apr 17, 2019
8.10.9 Apr 05, 2019
8.10.8 Mar 22, 2019
8.10.7 Mar 11, 2019
8.10.6 Feb 23, 2019
8.10.5 Feb 08, 2019
8.10.4 Jan 25, 2019
8.10.3 Jan 11, 2019
8.10.2 Dec 07, 2018
8.10.1 Nov 27, 2018
8.10.0 Nov 16, 2018
8.9.16 Oct 19, 2018
8.9.15 Oct 06, 2018
8.9.14 Sep 21, 2018
8.9.13 Sep 07, 2018
8.9.12 Aug 29, 2018
8.9.11 Aug 21, 2018
8.9.10 Jul 12, 2018
8.9.9 Jun 29, 2018
8.9.8 Jun 16, 2018
8.9.7 May 30, 2018
8.9.6 May 19, 2018
8.9.5 May 05, 2018
8.9.4 Apr 17, 2018
8.9.3 Apr 05, 2018
8.9.2 Mar 20, 2018
8.9.1 Mar 08, 2018
8.9.0 Feb 22, 2018
8.8.11 Feb 08, 2018
8.8.10 Jan 25, 2018
8.8.9 Jan 11, 2018
8.8.8 Dec 08, 2017
8.8.7 Dec 01, 2017
8.8.6 Nov 16, 2017
8.8.5 Oct 31, 2017
8.8.4 Oct 17, 2017
8.8.3 Oct 06, 2017
8.8.2 Sep 22, 2017
8.8.1 Sep 08, 2017
8.8.0 Aug 22, 2017
8.7.1 Aug 03, 2017
8.7.0 Jul 28, 2017
8.6.0 Jul 07, 2017
8.5.2 Jun 22, 2017
8.5.1 Jun 08, 2017
8.5.0 May 24, 2017
8.4.3 May 12, 2017
8.4.2 Apr 26, 2017
8.4.1 Apr 15, 2017
8.4.0 Apr 07, 2017
8.3.3 Mar 15, 2017
8.3.2 Mar 03, 2017
8.3.1 Feb 22, 2017
8.3.0 Feb 17, 2017
8.2.0 Feb 03, 2017
8.1.0 Jan 25, 2017
8.0.1 Jan 14, 2017
8.0.0 Dec 14, 2016
7.7.5 Nov 24, 2016
7.7.4 Nov 15, 2016
7.7.3 Oct 27, 2016
7.7.2 Oct 12, 2016
7.7.1 Sep 28, 2016
7.7.0 Sep 20, 2016
7.6.1 Sep 17, 2016
7.6.0 Sep 12, 2016
7.5.2 Aug 12, 2016
7.5.1 Jul 30, 2016
7.5.0 Jul 30, 2016
7.4.5 Jul 15, 2016
7.4.4 Jun 30, 2016
7.4.3 Jun 17, 2016
7.4.2 Jun 03, 2016
7.4.1 May 25, 2016
7.4.0 May 18, 2016
7.3.2 May 06, 2016
7.3.1 Apr 22, 2016
7.3.0 Apr 08, 2016
7.2.8 Mar 21, 2016
7.2.7 Mar 10, 2016
7.2.6 Feb 26, 2016
7.2.5 Feb 12, 2016
7.2.4 Jan 29, 2016
7.2.3 Jan 13, 2016
7.2.2 Dec 15, 2015
7.1.1 Nov 04, 2015
7.1.0 Oct 09, 2015
7.0.11 Sep 17, 2015
7.0.9 Aug 08, 2015
7.0.8 Jul 23, 2015
7.0.7 Jun 30, 2015
7.0.6 Jun 12, 2015
7.0.5 May 29, 2015
7.0.4 Mar 27, 2015
7.0.2 Jan 20, 2015
7.0.1 Nov 26, 2014
7.0.0 Nov 02, 2014
6.3.0 Sep 28, 2014
6.2.0 Jul 01, 2014
6.1.0 May 10, 2014
6.0.0 Feb 28, 2014
6.0.0a0 Mar 26, 2014
5.9.2 Dec 23, 2013
5.9.1 Dec 16, 2013
5.9b1 Nov 15, 2013
5.8b1 Sep 03, 2013
5.7b2 Jul 22, 2013
5.7b1 Jul 21, 2013
5.6b1 Jun 10, 2013
5.5b1 May 10, 2013
5.4b1 Mar 09, 2013
5.3b1 Jan 09, 2013
5.2b1 Oct 12, 2012
5.1b1 Sep 09, 2012
5.0b2 Jul 18, 2012
5.0b1 Jul 13, 2012
4.9b1 Jun 29, 2012
4.8b1 Apr 27, 2012
4.7b1 Mar 19, 2012
4.6b1 Feb 10, 2012
4.5b1 Jan 21, 2012
4.4b1 Dec 12, 2011
4.3b1 Nov 25, 2011
4.2b1 Nov 12, 2011
4.1b1 Oct 20, 2011
4.0b1 Oct 07, 2011
3.9b1 Oct 06, 2011
3.7b1 Jul 05, 2011
3.5b2 Jun 08, 2011
3.5b1 May 27, 2011
3.3a1 May 04, 2011

Wheel compatibility matrix

Platform Python 2 Python 3
any

Files in release

No dependencies